Advertisement

C++ Question

Started by February 15, 2001 10:52 AM
11 comments, last by frank2000 23 years, 11 months ago
Hi everybody, I have a base class Character. Classes Soldier and Monster inherit it. I store objects of Soldier and Monster in an array Character *ary[10]. My question is, how to access the data and functions of Soldier and Monster objcets using the Character pointer? Thank you very much. Frank
I hope I''m understanding you right on this. To access a data structure or class from a pointer, use -> instead of a period:

class cMyClass
{
public:
DWORD dwVar;
};

cMyClass *Mine;

Mine = new cMyClass();
Mine->dwVar = 10;
delete Mine;

All derived classes use the base class data as well:

class cMyDerivedClass : public cMyClass
{
public:
DWORD dwVar2;
};

cMyDerivedClass *Mine;

Mine = new cMyDerivedClass();
Mine->dwVar = 10;
Mine->dwVar2 = 10;
delete Mine;

Jim Adams

Advertisement
Correct me if I''m wrong.. but this is polymorphism right?

Class character has to have virtual functions like

virtual takeDamage(int damage);


then both Soldier and Monster can have their specific takeDamage()

then when you call character->takeDamage, the proper takeDamage() is called depending on wether or not the Character pointer is pointing to type monster or soldier. If either Monster or Soldier doesn''t have its own takeDamage() function, then the generic character::takeDamage() is called.

Right?
Hi Jim Adams,

Sorry for my poor English...what I mean is, in your example, use "cMyClass *Mine;" instead of "cMyDerivedClass *Mine;". If I code this, the compiler will complain that cMyClass doesn''t have member dwVar2.

Thanks for your attention.

Frank
Try this:

http://www.zib.de/Visual/people/mueller/Course/Tutorial/node7.html
  Character* character = new Soldier[10];character[0].whateverAttributesSoldierHas = x;character[0].whateverFunctionsSoldierHas();Character* other = new Monster[10];other[0].whateverAttributesMonsterHas = x;other[0].whateverFunctionsMonsterHas();  



"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
Advertisement
quote: Original post by frank2000

Hi everybody,

I have a base class Character. Classes Soldier and Monster inherit it. I store objects of Soldier and Monster in an array Character *ary[10].

My question is, how to access the data and functions of Soldier and Monster objcets using the Character pointer?

Thank you very much.

Frank



You can't access the data and functions of Soldier and Monster object using a Character pointer. The Character class doesn't _know_ anything about Soldiers or Monsters.

You can, however, convert the Character pointer into a pointer to a Soldier or a Monster object.

You should be able to use a C style cast:
Monster *pBadGuy = (Monster *) ary[0];

Or you could use the C++ style cast:
Monster *pBadGuy = static_cast < Monster * > (ary[0]);

And then you could access all of the data and functions of the Monster object through this pointer.

Remember that by doing this, you are bypassing C++'s type checking and you open yourself up for problems. If you convert ary[0] into a Monster pointer, but it really is a Soldier pointer, and then you start calling Monster functions or accessing Monster data, the behavior is undefined, which usually means that your program will crash.

You might want to think about using virtual functions.


Edited by - ShawnO on February 15, 2001 1:49:26 PM
When using the Windows calculator program, always remember to clear any values from memory before exiting to prevent burn-in.
Hi ncsu121978,

Thanks for your reply. But it has the same result - the compiler complains that Character doesn''t have the corresponding attributes and functions.

If I use ((Soldier *)Character[0]).whateverAttributesSoldierHas = x; The compiler will shut up. But how can I know that the pointer is currently pointing to Soldier or Monster, so that I can cast the pointer correctly?

My program is a real-time 3d game. It cannot affort comparisons before every assignments or function calls. And the worst thing is I discovered this problem when I''ve finished coding over 70% (because Smalltalk don''t has such problem so I assume that c++ is the same...and start coding). Does anyone know what should I do?
Thank you ShawnO. I think I should correct the whole program......
Take the data members that are common to both Soldier and Monster into Character instead. Then make the derived classes use the base class''s data members.

For example:



class Character
{
public:
// common stuff:
int HP;
int AmmoRemaining;
};

class Soldier : public Character
{
};

class Enemy : public Character
{
};

void Function(Character* p)
{
// Works for both soldiers and enemies
p->HP = 10;
p->Ammo = 10;
}

int main(int argc, char* argv[])
{
Soldier s1, s2, s3;
Function(&s1);
Function(&s2);
Function(&s3);

Enemy e1, e2, e3;
Function(&e1);
Function(&e2);
Function(&e3);
}

This topic is closed to new replies.

Advertisement