Jump to content
  • Advertisement
Sign in to follow this  
Kassandra

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

This topic is 5079 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
-> 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).

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by Pipo DeClown
pObject->foo

equals

(*pObject).foo


Yeah -> is actually "syntactic sugar".Less writing

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
(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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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 should
object.x = 2;
object.y = 2;
//array[2] = object; //No need

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!