Jump to content
  • Advertisement

Archived

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

vbuser

How to use a Pointer Point to a Class?

This topic is 5750 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

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
Advertisement
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
The Previous post is correct its:

(*Model1a).SetSpeed(50);

or

Model1a->SetSpeed(50);


"... thats the rub...

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

  • 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!