Sign in to follow this  
marshdabeachy

Inheritance

Recommended Posts

marshdabeachy    100
Well, here's my end goal. If someone has a better way to go about it, let me know. I'm writing a 2D DirectX game in C++ and am drawing sprites to the screen. I have all the sprites wrapped in a class called "spriteInfo" and they're being stuffed into a std::list<spriteInfo>, which throws everything on the screen using the spriteInfo structs. The game is 1-4 players, so I have another 1-4 sprites (the character(s)) that I want the user(s) to control. These sprites are wrapped in a class called "character", which inherits from "spriteInfo". They're being added to the spriteInfo list, so they're drawn to the screen like everything else. I need to be able to quickly access the data members of each character so I can update their positions in the world. So here's what I'm doing... I've got an array of iterator pointers (std::list<character>::iterator*), that theoretically should be pointing to the location of the iterator in the list where the character sprite(s) are being stored. However, I'm getting a compiler error when I try this. It looks like it's related to inheritance. Here's what the code looks like:
class spriteInfo {
public:
	float x, y, elevation, scale;
	float width, height;
	float distToCamera;
	IDirect3DTexture9* texture;
};
class character : public spriteInfo {
public:
	float angle;
	float angleAccel;
	float speed;
};

std::list<spriteInfo> spriteList;
characters = new std::list<character>::iterator*[cameraCount];
for (int i=0; i<cameraCount; i++) {
	character c;
	// character data populated here
	spriteList.push_front(c);
	characters[i] = &spriteList.begin();
}



Here is the error I am getting:
Quote:
error C2440: '=' : cannot convert from 'std::list<_Ty>::iterator *__w64 ' to 'std::list<_Ty>::iterator *' with [ _Ty=spriteInfo ] and [ _Ty=character ] Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
How would I go about getting this fixed?

Share this post


Link to post
Share on other sites
yaustar    1022
Switch to a list of pointers to sprites instead. I am not quite sure how to explain the reasoning though. I am afraid you have to wait for someone else to explain or look it up yourself.

Share this post


Link to post
Share on other sites
CTar    1134
character[i] is of type std::list<character>::iterator*
&spriteList.begin() is of type std::list<spriteInfo>::iterator*
The first is a pointer to an iterator of a list containing characters, but begin returns an iterator to a list containing sprite infos, so one iterator points to one kind of data and the other points to another kind of data. What you most likely want is dynamic polymorphism, to achieve this in C++ you need pointers. So spriteList should be a list of spriteInfo* and characters should be an array of pointers to iterators to a std::list containing spriteInfo* (yes you should use spriteInfo*, not character*).

Share this post


Link to post
Share on other sites
marshdabeachy    100
Okay, I've converted it over and it's still not working. I've got this...

std::list<spriteInfo*>::iterator* base = characters[i];
character* c = **base;

It's giving me this error:

Quote:
error C2440: 'initializing' : cannot convert from 'std::allocator<_Ty>::value_type' to 'character *'
with
[
_Ty=spriteInfo *
]
Cast from base to derived requires dynamic_cast or static_cast


I've never used dynamic_cast/static_cast before, and I wasn't able to get it working. Any more ideas? It's getting closer.

Share this post


Link to post
Share on other sites
marshdabeachy    100
Okay, I finally got it working. My "characters" array is now simply an array of character pointers, not iterator pointers. Much more simple.

[Edited by - marshdabeachy on May 21, 2006 3:33:56 AM]

Share this post


Link to post
Share on other sites

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