Skip to main content
gamedev.net
🔒 Locked

C++ & objects...

Started by platonman1 Oct 9, 2004 at 3:38 AM 2 replies 800+ views
Original Post
platonman1
platonman1
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 :)
My english is not the best... I already know that :)
Fruny
Fruny
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
platonman1
platonman1
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
My english is not the best... I already know that :)
Fruny
Fruny
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.