Reputation: 1575
I have a code in which I use a pointer to a class that I made. As an example lets say I have a Person method, and each Person had a name. Now lets say I use a pointer for a person like
Person& * person_;
Now lets say I'm trying to get the name of the person using the pointer I have. If I do something as simple as
person_.getName()
It will return an error saying that it's a non-class type.
My question is: if all I have is the pointer to work with, how do I use the methods of the class it's pointing to?
Upvotes: 0
Views: 116
Reputation: 119
person_->getName()
is what you should be using.
You can read about it on cplusplus.com
Upvotes: 0
Reputation: 19897
person_->getName()
is shorthand for (*person_).getName()
. Both will work. Also, as @Marcelo pointed out, it should be Person& * person_;
Upvotes: 1
Reputation: 185852
I don't how you got the compiler to even accept Person& * person_
, since that's a meaningless declaration (there are no pointers to references). Assuming you actually used Person* person_
, simply use the ->
operator: person_->getName()
.
Upvotes: 5
Reputation: 793
You use the pointer notation:
person_->getName()
->
is what you want.
Upvotes: 1