-
June 19th, 2009, 11:29 AM
#1
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.
-
June 19th, 2009, 11:37 AM
#2
Re: ordering of member access operator
It is the latter case: ((A->B)->C)->D).
-
June 19th, 2009, 07:31 PM
#3
Re: ordering of member access operator
-
June 19th, 2009, 07:50 PM
#4
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).
-
June 19th, 2009, 08:56 PM
#5
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
If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).
-
June 20th, 2009, 12:01 AM
#6
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...
Last edited by hajimeml; June 20th, 2009 at 12:04 AM.
-
June 20th, 2009, 12:24 AM
#7
Re: ordering of member access operator
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()
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.
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.
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.
-
June 22nd, 2009, 09:36 AM
#8
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?
-
June 22nd, 2009, 09:48 AM
#9
Re: ordering of member access operator
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?
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".
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.
-
June 27th, 2009, 11:55 PM
#10
Re: ordering of member access operator
Now I understood. Thank you all of you for your help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|