Difference between - and . for accessing members in C++

Started by
8 comments, last by Pipo DeClown 19 years, 8 months ago
I've been trying to figure that out for a while, have been looking around the web for answers and all. Coming from a programming background with languages where you either have -> or . to access members it was rather confusing for me to see that in C++ you have both. And in thinking about it and trying to find out when to use which I am only getting more confused... So, I'd greatly welcome any help any of you might be able to give me. Okay, so from what I understand you can use . to access members of structs and object and call methods of objects. But when you have a pointer to the object/struct you use -> instead. However when I started thinking about that, I came to believe that I must have misunderstood. When you create an instance of an object what you have in that variable that holds the instance is actually a reference to where the information of that object is saved, isn't it? And isn't a reference kind of the same as a pointer? That would then mean that I would never use . and always -> which definately isn't true... I think it is safe to say that I am utterly and hopelessly confused about this. I am sure it is a rather simple matter but I have this weird talent for getting the simplest concepts totally distorted and knoted up in my head... [rolleyes]
--Perfection, my messenger from hell.
Advertisement
pObject->foo

equals

(*pObject).foo

-> and . do almost the same thing, but -> requires a pointer type as its left argument, and . requires a reference type.

Thus, you can do this:

struct Point {  float x;  float y;};float foo( Point a, Point * pb ) {  return a.x +         pb->x +         (&a)->y +         (*pb).y;}


That's really all there is to it (unless you want to go into operator overloading).
enum Bool { True, False, FileNotFound };
Simple one this. The . operator is used to access members of a class/struct and the -> operator is used to access members of a class/struct via a pointer.

Example:

class CFoo{public:   int x, y;   CFoo(int x, int y) { this->x = x, this->y = y; }};int main(){   CFoo foo(5, 6);   CFoo *pFoo = new CFoo(5, 6);   foo.x = 7;   pFoo->x = 7;   delete pFoo; // Remember to match each use of new with a delete.   return 0;}


Barring any syntax errors that should compile and run, setting the x member of foo and pFoo to 7.

-hellz
Quote:Original post by hplus0603
-> and . do almost the same thing, but -> requires a pointer type as its left argument, and . requires a reference type.

Thus, you can do this:

struct Point {  float x;  float y;};float foo( Point a, Point * pb ) {  return a.x +         pb->x +         (&a)->y +         (*pb).y;}


That's really all there is to it (unless you want to go into operator overloading).

Cool. I didn't know you could do (&Object)->; Hehe.
Quote:Original post by Pipo DeClown
pObject->foo

equals

(*pObject).foo


Yeah -> is actually "syntactic sugar".Less writing
Ah, thank you! So, my actual problem was in seeing that there is a difference between a reference and a pointer... Hehe, sometimes I feel really dense. Ah, well. In order to learn one needs to make mistakes. [smile]

And again, thank you. As always, you people have been very helpful.
--Perfection, my messenger from hell.
(Adding stuff to what has been said)

The C++ -> operator can be overloaded (this is actually damn cool because it allow you to use smart pointers transparently), while . cannot.
Main use of pointers (as i see it at least) are to "update" stuff. For example, use a pointer to update the data stored in an array:

type *object = &array[2];
object->x = 2;
object->y = 2;

compared too:
type object = array[2];
object.x = 2;
object.y = 2;
array[2] = object;

(just adding that for someone wondering the use of pointers)
Roger that, lets run like hell!
Quote:Original post by Interesting Dave
Main use of pointers (as i see it at least) are to "update" stuff. For example, use a pointer to update the data stored in an array:

type *object = &array[2];
object->x = 2;
object->y = 2;

compared too:
type object = array[2];
object.x = 2;
object.y = 2;
array[2] = object;

(just adding that for someone wondering the use of pointers)


compared too:type object = &array[2]; //I don't know if this compiles, it shouldobject.x = 2;object.y = 2;//array[2] = object; //No need

This topic is closed to new replies.

Advertisement