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
It would be nice, instead of gathering scattered information on Google (who doesn't understand symbols), to ask the many programming professionals on this site.
I am wondering about a few operators:
x->y vs (*x).y
x.*y vs *(x.y)
x->*y vs *(x).*y vs *(*(x).y) vs *(x->y)
Compared to each other, is one or the other faster?
Number 1 is just for convenience and already existed in C. Consider:
x->y->z
vs
(*(*x).y).z
The other two operators don't do what you think they do.
x.*y
calls the member function pointed to by y on the object referenced by x while
x->*y
does the same thing but for x being a pointer instead.
Here is an example of calling a member function through a pointer:
struct Foo {
void bar();
};
int main() {
typedef void (Foo::*foo_mem_ptr)();
// y points to Foo::bar
foo_mem_ptr y = &Foo::bar;
Foo x;
// call y with x as this
x.*y();
// same for pointer to x
Foo *px = &x;
px->*y();
}
As far as I know there is absolutely no difference between those operators in basic implementation - they are equivalent. However, You can override both * and -> to have different behaviour.
They are equivalent, it's just the syntactic sugar.
It's 1st and 2nd case mixed together.
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 1 year ago.
Improve this question
Seeing that QT has a piece of code like this, how can new return an object instead of a pointer?
You made the wrong conclusion. new always returns a pointer. You need to read the documentation of the classes you are using to know how their constructor works. One way to enable foo f = new foo; is the following:
struct foo {
foo(foo*){}
foo(){}
};
int main(){
foo f = new foo;
}
Note that = here is not assignment! The object f is initialized by calling its constructor taking a pointer to a foo.
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 need to get clear about, the best way to call the super class constructor/method explicitly.
I tried with the following both way to call superclass constructor:
Myclass::Myclass(int a, int b):X(a),Y(b)
{
// do something
}
and
Myclass::Myclass(int a, int b)
{
X = a;
Y = b;
}
So my question over here is:
Which is the best way to call super class constructor/method explicitly?
And what are the benefits will get in both way?
what is the best practice and why?
There is any performance issue lies with both way?
Regarding my question i found this link:
What are the rules for calling the superclass constructor? but still i have little more doubt what i asked above.
if there is any online tutorial, blog or video also u can mention over here, it will great help full for me. Thank in advance.....
The only correct way to call superclass's constructor is from the initialization list:
Myclass::Myclass(int a, int b)
:X(a),Y(b)
{}
The other way in fact calls different constructors:
Myclass::Myclass(int a, int b)
// implicit :X(),Y()
{
// These two don't call constructors but actually declare variables
X(a);
Y(b);
}
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
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.