ordering of member access operator
Hello. I have a question about the member access operator ->
I am trying to understand a code of this format: A->B->C->D.
Does it means A->(B->(C->D)) or ((A->B)->C)->D)?
In the former case, I shall try to understand the code which is represented by (C->D) first.
Thanks.
Re: ordering of member access operator
It is the latter case: ((A->B)->C)->D).
Re: ordering of member access operator
Why writing the code this way?
By the way, what are the advantages of writing the code this way? It seems that the author of the program uses this method to make a pointer (a private member) of one class to point to another class (pointed to by a private member defined in another class).
Re: ordering of member access operator
The answer to your question is actually part of the study of C++; there are many tributaries. It's not so much that it's and advantage as it is a technique.
The idea may be that an object needs to 'have' some objects, which in your example, have some object, and that has some objects, too.
Without anything telling me the types of A, B, C or D, all I can say is that it LOOKS like they're pointers (-> can be an operator overload, a function that returns a pointer).
A might be a car. B, which A 'has' and is pointing to in
A->B
Might be a passenger. C might be a wallet, owned by the passenger as indicated by
A->B->C
D might be his credit card.
So, if you want the credit card from passenger B's wallet, you find him in car A with
A->B->C->D
Re: ordering of member access operator
Thanks for the very interesting example and explaination. I found another variation of the code in the program. Here, the type of A-D are as follows:
A->PGElementPtr()->WPtr()->GetSize()
where PGElementPtr(), WPtr() and GetSize() are respectively B,C,D in the original post.
A is a private member of TFormMain. It points to class C1.
In class C1, there is a member function PGElementPtr().
This function returns a pointer pointing to class C2.
Such pointer is called PGElement which is a private member of class C1.
PGElement in class C1 calls the member function WPtr() of class C2.
WPtr() returns a pointer to class C3. Such pointer is called w, which
is a private member of class C2.
GetSize() is a virtual member function of C3. It has resize(size) in
the function definition which I guess returns the size of class C3.
I am confused by such complicated statement. Do you have an idea what it actually does?
Is the prgrammer trying to get the size of class C3 from TFormMain? I am not sure why he goes through so many steps by using a series of ->. Also, is there a side effect of this statment? I am not sure if he is setting up the links to classes for other parts of the program to use...
Re: ordering of member access operator
Quote:
Originally Posted by hajimeml
Do you have an idea what it actually does?
Take it step by step:
Get the PGElement, of type C2*, from A:
A->PGElementPtr()
Get the WPtr, of type C3*, from the PGElement:
A->PGElementPtr()->WPtr()
Call member function GetSize() on the object that the WPtr points to:
A->PGElementPtr()->WPtr()->GetSize()
Quote:
Originally Posted by hajimeml
Is the prgrammer trying to get the size of class C3 from TFormMain?
I am cautious with the term "size of class" because that may mean the use of the sizeof operator, whereas here a GetSize member function of the C3 class is called. However, I think that you have the general idea right.
Quote:
Originally Posted by hajimeml
I am not sure why he goes through so many steps by using a series of ->.
How would you do it otherwise? You could break it down into a series of statements, but then you would need to introduce a couple of named variables.
Quote:
Originally Posted by hajimeml
Also, is there a side effect of this statment? I am not sure if he is setting up the links to classes for other parts of the program to use...
Unfortunately, this is impossible to tell without further examination of the source.
Re: ordering of member access operator
laserlight, thank you very much for your help. Could you please clarify the following?
I am still a bit confused.
>Get the WPtr, of type C3*, from the PGElement:
>A->PGElementPtr()->WPtr()
Maybe I misunderstood. Could you please let me know why it is not "Get the w, of type C3*, from the PGElement?
>Call member function GetSize() on the object that the WPtr points to:
>A->PGElementPtr()->WPtr()->GetSize()
Also, why it is not "Call member function GetSize() on the object that the w points to"?
>How would you do it otherwise? You could break it down into a series of statements, but then >you would need to introduce a couple of named variables.
Can I just introduce a pointer, say C3ptr, to class C3 within TFormMain. Then, execute
C3ptr->GetSize() inside TFormMain?
Re: ordering of member access operator
Quote:
Originally Posted by hajimeml
Maybe I misunderstood. Could you please let me know why it is not "Get the w, of type C3*, from the PGElement?
Quote:
Originally Posted by hajimeml
Also, why it is not "Call member function GetSize() on the object that the w points to"?
You can substitute "WPtr" with "w".
Quote:
Originally Posted by hajimeml
Can I just introduce a pointer, say C3ptr, to class C3 within TFormMain. Then, execute
C3ptr->GetSize() inside TFormMain?
Yes, but is such a member variable necessary? I had in mind variables local to the function in which A->PGElementPtr()->WPtr()->GetSize() was called.
Re: ordering of member access operator
Now I understood. Thank you all of you for your help.