Sign in to follow this  

C++ & objects...

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

Hi, I have a problem with C++ objects. Say I have a Base class, and two other classes, for example C1 and C2. It's something like that: class Base { public: Base() {} ~Base() {} private: C1 class1; C2 class2; }; How can I access class1 member functions from class2 ??? Thanx in Advance :)

Share this post


Link to post
Share on other sites
Quote:
Original post by platonman1
How can I access class1 member functions from class2 ???


By passing a reference (or pointer) to class1 around from within your Base object, whenever you need one, or during construction of class2.

There are hacky things you could do, involving pointer arithmetic, but they are dangerous.

C1 has, a priori, no knowledge that it is being used from a Base object, and no knowledge that it has a C1 object nearby. Indeed, somewhere else in your code, it might not be the case.

Share this post


Link to post
Share on other sites
Does it mean that my conception is bad ???

Well, if it's the case, I explain the problem : I have a Terrain class and a Camera class. In the first implementation of my Terrain class, I simply display a brute-force terrain and, for each polygon, I have to know if it is in the viewing frustum of the camera...And I don't want to have a Camera membre in the Terrain class.

So what can I do to avoid those hacky things ? :p

Share this post


Link to post
Share on other sites
You could put your rendering code to the Base class and have it coordinate between Terrain and Camera, passing terrain data to camera functions that need it and vice-versa.

For best results, you will have to ask in the Graphics forum, people there are probably more experimented at organizing a renderer's code.

Share this post


Link to post
Share on other sites

This topic is 4814 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this