Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I am working on OOPs and using C++. I have one class accessing object of other class/struct.
struct data
{
int a;
int b;
string str;
} sd;
class format
{
int x;
void show()
{
cout << data.a << endl;
}
};
which one is best to use here class or struct?
First of all, it's struct, not strut.
Second, you cannot access member a like you do, data.a, but rather sd.a, because you need to access it on an instance, not on the name of the struct.
For the detailed differences between class and struct see this SO question and its two best rated answers.
I use this convention:
A struct only have members that it make sense to manipulate directly
A class may have complicated rules for assigning members
This somewhat fits well with the default accessibility rules. But as said before in this thread, the choice depends on convention.
that depends on your requirement the only difference in struct and class is in struct all members are public by default and private in case of class
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I new to programming. In stack overflow i couldn't see difference between : & :: is mentioned. Could anyone can explain in detail it helps to beginner learners like me. Thank you.
So you would use :: when you're defining/using methods from a class, so like for example
class foo{
public:
int bar;
int hi(int x);
int func(); // static member function
Foo(int num): bar(num) {}; // use of a colon, initialization list
};
int foo::hi(int x){
//define the function
}
Also if you have static member functions, you can just call those whenever through using foo::func(). You can find more about static member functions online.
The single colon is for member initialization list (you can look this topic up online) where you can initialization member variables in the construction of your class.
You can also find single colon used in polymorphism, when you derive a class from a base class. You can find more information about c++ polymorphism online.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I read about defining a struct like:
struct someStruct {
int x;
int y;
};
struct otherStruct : public someStruct {};
So my question is about the definition of otherStruct.
What does this definition do?
I'm new in C++ so i only want to know under which key word i can find the definition of otherStruct to read about them in a book.
This answer is just a scratch on the surface of the inheritance concept of OOP and it does not cover all its aspects. You should read a book about C++ (or about OOP in general) to get a complete answer.
The part struct otherStruct : public someStruct says that otherStruct extends someStruct with public inheritance. In simple words, public inheritance does not change the visibility of the members (properties and methods) inherited from the base class.
The declaration block of the new struct ({}) is empty. It does not add any new members to those inherited from struct someStruct.
If you compare someStruct and otherStruct by their memory footprint and behaviour, they are identical. But they are different types and they cannot be replaced one for the other.
However, a pointer to a variable of type otherStruct can be used where a pointer to struct someStruct is expected (because otherStruct, by extending someStruct has all the properties expected from someStruct) but the other way around is not possible.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm creating class that has a templated object (Item<T>) as a member, basically like this:
class myClass
{
int other_int;
public:
int member_function();
vector<Item<T>> vec;
};
Currently, I have Item<string>, but I need to be able to use it with non string objects. Is there a way to do this without templating myClass (which would obviously be a lot of work for a complicated class)?
If your class will only use Item< string>, you may try:
class myClass
{
int other_int;
public:
int member_function();
vector<Item<string>> vec;
};
But if you want any other type of Item in the vector, the answer is No, there is no magic solutions.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Is it bad to call global functions from member functions of a class? I mean is this ok...
class MyClass
{
void print_numb();
};
int get_numb()
{
return 10;
}
void MyClass::print_numb()
{
cout << get_numb() << endl;
}
If get_numb() is only used by a source file implementing MyClass then I'd put it in an anonymous namespace in that source file:
namespace /*no name here means the namespace is anonymous*/ {
int get_numb()
{
return 10;
}
}
That hides it away. I prefer that to a static private function in the class since it reduces the amount of stuff in the class declaration.
It is absolutely OK from the technical point of view: free-standing functions, global and static, are part of the language. There is no reason not to use them.
It is also OK stylistically: the Standard C++ Library provides free-standing functions, so the designers of the language were definitely OK with the idea of mixing member and non-member functions.
Of course you should take advantage of the C++ features that let you isolate your functions from functions in the libraries to which you link - by reducing their visibility to a single translation unit, or by placing them in a namespace.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
My question is simple, what is the performance loss due to reference length. I cannot explain myself but here is the sample:
between this
C* pC = m_a->m_b->m_c;
and this expression
C* pC = m_b->m_c;
I am asking this because I have a global class which has a Singleton pattern and holds everything. I am accessing all of its members from its members like this.
class Global
{
A* a;
X* x;
};
class A { B* b; };
class B { C* c; }; // etc
class X { Y* y; };
class Y { Z* z; };
class Z
{
void foo() { Global::GetInstance()->a->b->c->foo(); }
}
Is this a good design? Any advice for this? I am having some trouble with this topic too Qt Architecture Advice Needed
Every -> operator is an indexed indirection, which costs a cycle or two, depending on the processor, and may be invisible if its pipeline is good enough.
However the real question here is 'compared to what?' What other implementation techniques are you considering for solving this problem? Unless you have a viable alternative your question is really meaningless.
Similarly the frequently-asked question about the relative efficiency of virtual and non-virtual functions is meaningless unless it takes into account how to get the same effect both ways. In the non-virtual case this amounts at least to an 'if' or 'switch', whose cost has to be added in to the comparison.