Page 1 of 1

Polymorphism in C++

PostPosted: Aug 14, 2003 @ 3:40am
by fzammetti
I'm trying to get something to work that is trivial in Java (which is what I code in most of the day at work), but I can't seem to get it to work in C++...

What I want to do is have a class, CScreen, that I will inherit from to create classes for specific screens in my game.

What I'd like to be able to do is have a variable,

CScreen* currentScreen;

defined in my program, and then be able to do:

currentScreen->mainDraw();

where mainDraw() will be the main drawing function called to draw every frame, as an example.

Obviously, as currentScreen is changed to point to a different CScreen-derived class, I still want to be able to call it's mainDraw() function. In other words, I want to do the equivalent of this Java snippet:

class CScreen {
public void mainDraw() { }
}
class mainMenu extends CScreen {
public void mainDraw() { }
}
public class test {
public static void main(String args[]) {
CScreen currentScreen;
currentScreen = new mainMenu();
currentScreen.mainDraw();
}
}

See, simple polymorphism, but I'm not clear on how to implement that in C++. Any help? Thanks all!

PostPosted: Aug 14, 2003 @ 3:55am
by Kzinti

PostPosted: Aug 14, 2003 @ 3:55am
by Digby
Start

PostPosted: Aug 14, 2003 @ 4:03am
by fzammetti
Thanks guys, I'll go give that a shot. What I had wrote in trying to get it to work today looks almost identical to what you wrote, Thierry, I must have just had some relatively minor syntactical problem that I didn't pick up on. Nice to know I was on the right track anyway. Thanks again!

PostPosted: Aug 14, 2003 @ 4:21am
by rcp
ya, what you wrote is close, but you left out
'virtual'
If you are going to really use the features of C++, I suggest that you actually learn it... not just how to do a couple of tricks. This is not a slam, rather a voice of experience. C++ will quickly let you code yourself in a hole from which there is little escape. :) It is a great language if you take the time to learn it and it can be a nightmare if you don't.

Best of Luck!

-rcp

PostPosted: Aug 14, 2003 @ 4:33am
by fzammetti
I in fact had used virtual in what I wrote this afternoon. In fact, from reading all this I'm at a total loss to explain why it didn't work because it was pretty much dead-on. I'm going to try again later when I'm done with what I'm currently doing.

As someone with over 20 years programming experience, I can assure you this isn't someone just learning a couple of tricks. I freely admit I'm fairly new to C++, but this was strictly a question about syntax. Conceptually I know what I'm doing quite well, even with my lack of C++ specific experience, I just wasn't sure how to write the code correctly from a syntactical perspective. This is actually one of the few instances where I've wanted/needed to use C++ techniques, meaning most of my code is straight C with very little OOP (for my personal projects I mean... I do Java professional all day long), so this is one of the few times my lack of experience has been a problem.

Thierry's code is correct

PostPosted: Aug 18, 2003 @ 12:28am
by mlepage