Can you put multiple types of classes in the same array?

Started by
19 comments, last by Zukias 11 years ago

Okay, so I have this ENEMY class right? I guess for simplicity, the subclasses are RED, BLU, GRN.

My game wants it to be that there is a certain chance that some of these varities spawn more than others, and I store all the enemies in a vector array.

The problem here is that if I make a vector array of ENEMY pointers, I can't access the member functions that are exclusive to the RED, BLU, and GRN subclasses. I can only access the base class' member functions, and even if I've overloaded RED/BLU/GRN's functions to behave differently, they default to the base class' methods. If this is easily explainable, or if there's a solution to this problem, please let me know; otherwise, I'll post my code and we'll see what we can do.

Advertisement

Are you declaring your overrides virtual? If you don't it will call the implementation based on the actual pointer type in the array. If they are virtual, everything will work as intended.

EDIT: They need to be virtual in the base class for this to work. The derived classes will automatically be virtual then. If you don't declare the base class function virtual it won't work either.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Yeah, I declared it virtual. It's telling me the ENEMY class' move() function doesn't take only one argument, when the RED object that is in the array is supposed to.

let's say:


class ENEMY
{
   friend class RED;  

   public:
      virtual void move(param1, param2);
}

class RED : public ENEMY
{
   public:
      void move(param1);
}

int main()
{
   vector<ENEMY*> ENEMIES;
   ENEMIES.push_back(new RED);
   ENEMIES[0]->move(param1);
   return 0;
}
 

It's giving me this error:

error C2660: 'ENEMY::move' : function does not take 1 arguments


It's not an override unless it takes the same number and type of parameters. You need to add a second parameter to the function in the derived class.

EDIT: And you don't need to declare your derived class a friend of the base class, there are no private methods or members and you can make them protected instead if you need to access them in a derived class.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

I also can't use a function exclusive to my subclass, right? If it's in this array, at least.

Is it acceptable to just have a junk parameter? I literally have no use for whatever I'd make as the second parameter.

Yes, have a parameter that isn't used in the function. Don't give it a name if you get a warning about the parameter not being used.

Why does the base class implementation take more parameters anyway?

EDIT: You can use a function exclusive to the subclass IF you call it from your virtual function in the derived class. You can also use dynamic_cast to get the real type but that isn't a good design.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

The regular ENEMY objects require the second parameter to seed its RNG when it chooses a random direction to move in that is distinct from all the other ENEMY objects.

The RED subclass moves towards specified coordinates, and thus doesn't need a seed for any RNG.

I also can't use a function exclusive to my subclass, right? If it's in this array, at least.

Is it acceptable to just have a junk parameter? I literally have no use for whatever I'd make as the second parameter.

How long have you been programming with C++? It doesnt seem like you have a firm grasp of polymorphism. If that's the case, you might want to really brush up and get a good idea of how c++ polymorphism works before you code with it.

But to answer your question, if you're trying to find ways to hack your code to get what you want, it likely means that your design is flawed.

While casting is not usually a great idea, it sounds like maybe it could help you here. Another alternative might be a message-passing system to get the behavior out of your child classes through virtual method calls on the base.

The regular ENEMY objects require the second parameter to seed its RNG when it chooses a random direction to move in that is distinct from all the other ENEMY objects.

The RED subclass moves towards specified coordinates, and thus don't need a seed for any RNG.

Why are you trying to seed your RNG every time you're going to use it?

The regular ENEMY objects require the second parameter to seed its RNG when it chooses a random direction to move in that is distinct from all the other ENEMY objects.

The RED subclass moves towards specified coordinates, and thus don't need a seed for any RNG.

You're passing a random seed every time you call move on an ENEMY? Why don't you set the seed once when the enemy is created?

See my edit to my post above re calling derived class methods as well.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

This topic is closed to new replies.

Advertisement