Tuesday, July 14, 2009

What is difference between "member selection" (.) and "pointer to member" (->) in C++?

This is a very basic C++ question, but help is appreciated.





I'm an experianced VB.net programmer, and want to quickly learn C++. The biggest hurdle at the moment is finding the new syntax and understanding unmanaged pointers - I already understand OOP very well.





Could anyone explain simply what the "pointer to member" operator really is doing? I'm used to only using member selection. Why is a different symbol needed?

What is difference between "member selection" (.) and "pointer to member" (-%26gt;) in C++?
Pointers are one of the most confusing concepts for some people. Not everyone. Just some people. So here is a description of pointers to make sure you get it. It is central to this entire discussion.





You should also know about the %26amp; operand which is used to get the address of something. To get the address of i, you would write %26amp;i. As in:


int i = 10;


int * p = %26amp;i;


now p contains the address of i.





A pointer is a variable that contains an address to memory somewhere on the computer. Let's say i is an integer with a value of 10. That integer lives somewhere in the computer RAM. Lets say it lives at address 5555. If we have a pointer p that points to that integer, then p has a value of 5555.





Great, let's go on.





Let's say there is a structure or class containing an integer i.


so i, is a member of our class. If an instance of that class exists somewhere it has an address. The same address and pointer discussion from before applies. p = 5555, which is the address of the object(instance of class). So we say p points to the object.





This might look like this:


MyClass o = MyClass(); //create object o


MyClass *p = %26amp;o; //get the address of o





Now we want to access the member i of that class. I would put p-%26gt;i to get that value because p is a pointer to the object that contains the member item I want. At any time, you can change p to make it point to some other object.





Member selection using "." is like the original integer discussion using i.


In the original discussion, i was the actual integer. In the same way, if the object is o, then o.i is the member i of object o.





In the example above, p-%26gt;i is refers to the same value as o.i;
Reply:Their is a difference, if the . operator is used, then the memory address that is manipulated is the address of the object being accessed plus the offset where the data is stored. If the -%26gt; operator is used, then the value of the pointer plus the offset is used to access the correct memory location.





Also, in C++ we can overload operators, including -%26gt;, which can allow for some interesting syntax.
Reply:Pointer to member operator (--%26gt;)will call the methods or members at runtime(dynamically). In C++ we have virtual functions . To call those functions we use pointer to member.





Where as we use member selection where call class members at compile time.


No comments:

Post a Comment