PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Polymorphism in C++

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!
Code:


class Screen
{
virtual void Draw();
};


class MyScreen : public Screen
{
virtual void Draw();
};


int main()
{
Screen* screen = new MyScreen;
screen->Draw();
}

Start here.
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!
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
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.
mlepage · 17 Aug 2003, 23:28 UTC — Thierry's code is correct
Thierry's code is correct, it should work.

If you really want to learn C++, there is no substitute for getting and reading in its entirety Stroustrup's book The C++ Programming Language 3rd edition.

I know, it's long, and some people believe it is hard to read, but don't believe the hype. It is one of the most well-written books I have read (and I have read almost a hundred and reviewed almost 20). The language C++ itself, some people like to diss, but I mean it when I say there really is no substitute.