This site is no longer active and is available for archival purposes only. Registration and login is disabled.

Polymorphism in C++


Polymorphism in C++

Postby fzammetti » Aug 14, 2003 @ 3:40am

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!
...and so I said to Mr. Gates: "$640 billion should be enough for anyone!"
User avatar
fzammetti
pm Insider
 
Posts: 1496
Joined: Jun 4, 2002 @ 6:21pm
Location: Omnytex Technologies


Postby Kzinti » Aug 14, 2003 @ 3:55am

Last edited by Kzinti on Aug 14, 2003 @ 3:56am, edited 3 times in total.
Kzinti
pm Member
 
Posts: 3238
Joined: Jan 13, 2002 @ 5:23am


Postby Digby » Aug 14, 2003 @ 3:55am

Start
Digby
pm Insider
 
Posts: 1011
Joined: Apr 29, 2001 @ 1:53pm


Postby fzammetti » Aug 14, 2003 @ 4:03am

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!
...and so I said to Mr. Gates: "$640 billion should be enough for anyone!"
User avatar
fzammetti
pm Insider
 
Posts: 1496
Joined: Jun 4, 2002 @ 6:21pm
Location: Omnytex Technologies


Postby rcp » Aug 14, 2003 @ 4:21am

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
User avatar
rcp
pm Member
 
Posts: 184
Joined: Jul 18, 2003 @ 2:12am
Location: Duluth, GA. (Southeast US)


Postby fzammetti » Aug 14, 2003 @ 4:33am

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.
...and so I said to Mr. Gates: "$640 billion should be enough for anyone!"
User avatar
fzammetti
pm Insider
 
Posts: 1496
Joined: Jun 4, 2002 @ 6:21pm
Location: Omnytex Technologies


Thierry's code is correct

Postby mlepage » Aug 18, 2003 @ 12:28am

User avatar
mlepage
pm Insider
 
Posts: 1050
Joined: Aug 3, 2003 @ 4:47am
Location: Canada


Return to Windows Mobile


Sort


Forum Description

A discussion forum for mobile device developers on the Windows Mobile platform. Any platform specific topics are welcome.

Moderators:

Dan East, sponge, Digby, David Horn, Kevin Gelso, RICoder

Forum permissions

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum