Archived

This topic is now archived and is closed to further replies.

vbuser

How to use a Pointer Point to a Class?

Recommended Posts

As the subject... How can I make a Pointer Point to a Class? e.g.: CAR is a class I''ve tried void main() { CAR Model1; CAR *Modedl1a; Model1a = &Model1; *Model1a.SetSpeed(50); cout << *Model1a.GetSpeed(); } That does not work....... I got compile error on: *Model1a.SetSpeed(50); cout << *Model1a.GetSpeed(); then what can I do? Please Help ME! I''''m a beginner..

Share this post


Link to post
Share on other sites
If that''s the exact code you compile, then your problem is a typo...

Try looking at the pointer definition....

If not, try posting the error message you get....

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Try either:

(*Model1a).SetSpeed(50);

or

Model1a->SetSpeed(50);

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
thx!
It work when using (*Model1a).XXXX

I just don''t know need the "()" for the ponter...

Share this post


Link to post
Share on other sites
The reason you need the parentheses is that . has a higher precedence than *. Thus, C++ thought you wanted to find the GetSpeed member of an object called Model1a, and then dereference it as a pointer, when in fact you wanted to do the opposite.

Don''t use the "(*this).that" notation. Use "this->that". It''s terser, cleaner, and more standard.


Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites