How To Use -> in C++?

Hello Professionals,

I would like to ask if you can explain in an easy way how to use this -> in C++? Thanks.
Last edited on
http://en.cppreference.com/w/cpp/language/operator_member_access

You have a pointer to class/struct type object. Object has members.
pointer->member
What is the vital purpose of pointers * and & in this example:

1
2
3
Entity *ptr = &e;
Entity& entity = *ptr;
entity.Print();


if I can always use like this???

1
2
Entity e;
e.Print();


What is the advantage of these?
I think that the "vital purpose" in such minimal example is "show syntax".
Yeah; the example code you've posted doesn't need to use pointers and references at all. It's just been done that way to give you an example of the syntax.

These days, talking about pointers can be... controversial. In C , they were an essential part of the language. Traditional C++ added references, which could be used to solve some of the same problems as pointers, but pointers were still an important part of the language. However, the various revisions of the standard over the last decade have introduced lots of new features to replace pointers, and many people now consider it bad practise to use traditional pointers when you can use the new features instead.

However, it's hard to understand what those new features are, and how to use them, without first understanding what pointers are, and how they work. So most teachers and courses will still teach pointers, and expect students to learn them, even if they then move on to the modern stuff.

Ignoring that discussion... if you're wondering why we might use pointers in programming, here's an old thread thread in which I listed several reasons why you might want to use them in traditional C++:

http://www.cplusplus.com/forum/general/105593/
Thanks for the insights pals. God bless. :)
Topic archived. No new replies allowed.