Sign in to follow this  

C++ inheritance and const confusion

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

Hello, so I've done a basic game of Pong and decided to move onto a game that's more complex, an arcade style shooter game. However, I'm having some troubles figuring out the whole organization of the game with inheritance and I've run into a few problems (I can get it working, but it required code I didn't like to get it to work). Basically I've got my game engine and game loop that handle the game cycles and input, the problem I'm having is with the inheritance of the sprites. Here's the structure of my sprites so far: (ones with - under them mean that the class with the - is derived from above classes) Sprite (basic sprite class that holds information relevant to any in game sprite) -Monster -Monster2 -MainCharacter I'm going to reorganize it to end up like: Sprite (basic sprite class that holds information relevant to any in game sprite) -Monster (pure virtual) -Monster1 -Monster2 -MainCharacter The problem that I'm having is figuring out how to share sprite pointers, so for example that the monsters would know where the main character is (so they can shoot at the main character). What I thought I could do would be make the main character a global pointer in the header for the main game, and then in the monster class put an extern pointer to the maincharacter with type Sprite. But for some reason that's not working out for me, it tells me that it's a re-declaration and that the type isn't right (since it was expecting MainCharacter* type instead of Sprite*). IE in the monster header file I want to have:
extern Sprite* g_pMainCharacter;
But to get it to work I have to have:
#include "MainCharacter.h"
extern MainCharacter* g_pMainCharacter;
So with inheritance I'm sure there's a way to do this, but I'm not doing it correctly. Is it because in my main header the g_pMainCharacter is declared as a MainCharacter* rather than a Sprite* (IE should i make it a Sprite* and then when I actually instantiate it use "= new MainCharacter ...")? Another minor thing I'm wondering is what is the proper way to declare constants for classes? Should the constant be in the class declaration (IE in the braces after class Monster { ) or should they be before the class declaration? I'd think they should be before the class declaration but I'm a bit unsure about how consts work. I've had some serious trouble when trying to use the extern keyword on a const, is this just not done? Should all my global consts be in a separate header that I include in any file that needs them (currently I've just been re-declaring them manually which is really stupid)? Any insights on any of these issues would be greatly appreciated.

Share this post


Link to post
Share on other sites
I think this topic will be related to what you want to know :P.
http://www.gamedev.net/community/forums/topic.asp?topic_id=483116

And wich API do you use for graphics? (SDL, Allegro, etc...)

Share this post


Link to post
Share on other sites
Thanks for the reply, right now I'm actually just purely using the Windows API since it supports some basic bitmap operations. I plan on moving on to DirectX after this game is complete.

The link you gave me is helpful and gave me some ideas, but it doesn't fully answer my questions above. I already have a sort of similar setup to that described in the link (I attach sprites to the engine to let it deal with them). I also understand the inheritance structure described in there, but I'm still unsure about passing the references around to other objects (since right now it gives me type errors when I compile).

Share this post


Link to post
Share on other sites
you can pass in your player as a sprite to the monsters.
IE.
(seems like this is the design you kinda have going)
class Player : public Sprite{
...
}
class Monster : public Sprite{
Sprite* hunted;

Monster(Sprite* p) : hunted(p){
...
}
}

main(){
Player* foo = new Player();
Monster* bar = new Monster(foo);
bar.hunt();
}

this should work as long as Sprite exposes everything a monster can do to a player. You can even make the hunted* a player or perhaps a monster and inherit player from monster.

Quote:
Another minor thing I'm wondering is what is the proper way to declare constants for classes? Should the constant be in the class declaration (IE in the braces after class Monster { ) or should they be before the class declaration? I'd think they should be before the class declaration but I'm a bit unsure about how consts work. I've had some serious trouble when trying to use the extern keyword on a const, is this just not done? Should all my global consts be in a separate header that I include in any file that needs them (currently I've just been re-declaring them manually which is really stupid)?


You should avoid using extern and globals. Try to keep things inside of classes and pass them in by reference or pointer. Generally there aren't many good reasons to use extern in c++. If you really need globals you can wrap them around in a class as static members.

[Edited by - fishmd on February 17, 2008 5:03:09 PM]

Share this post


Link to post
Share on other sites

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