Question:
What is difference between "member selection" (.) and "pointer to member" (->) in C++?
2008-01-22 05:39:35 UTC
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?
Three answers:
TristanVR
2008-01-22 15:52:51 UTC
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 & operand which is used to get the address of something. To get the address of i, you would write &i. As in:

int i = 10;

int * p = &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 = &o; //get the address of o



Now we want to access the member i of that class. I would put p->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->i is refers to the same value as o.i;
Pfo
2008-01-22 07:33:02 UTC
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 -> 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 ->, which can allow for some interesting syntax.
Ravi
2008-01-22 05:48:49 UTC
Pointer to member operator (-->)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.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...