Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
20 replies to this topic

#1 Akashi   Members   -  Reputation: 268

Like
0Likes
Like

Posted 21 April 2013 - 02:31 AM

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.



Sponsor:

#2 Paradigm Shifter   Crossbones+   -  Reputation: 5257

Like
0Likes
Like

Posted 21 April 2013 - 02:35 AM

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.


Edited by Paradigm Shifter, 21 April 2013 - 02:36 AM.

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

#3 Akashi   Members   -  Reputation: 268

Like
0Likes
Like

Posted 21 April 2013 - 02:56 AM

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


 


Edited by StoneMask, 21 April 2013 - 04:05 AM.


#4 Paradigm Shifter   Crossbones+   -  Reputation: 5257

Like
0Likes
Like

Posted 21 April 2013 - 03:01 AM

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.


Edited by Paradigm Shifter, 21 April 2013 - 03:03 AM.

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

#5 Akashi   Members   -  Reputation: 268

Like
0Likes
Like

Posted 21 April 2013 - 03:05 AM

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.



#6 Paradigm Shifter   Crossbones+   -  Reputation: 5257

Like
0Likes
Like

Posted 21 April 2013 - 03:06 AM

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.


Edited by Paradigm Shifter, 21 April 2013 - 03:11 AM.

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

#7 Akashi   Members   -  Reputation: 268

Like
0Likes
Like

Posted 21 April 2013 - 03:11 AM

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.


Edited by StoneMask, 21 April 2013 - 03:15 AM.


#8 0r0d   Members   -  Reputation: 815

Like
0Likes
Like

Posted 21 April 2013 - 03:13 AM

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.



#9 0r0d   Members   -  Reputation: 815

Like
0Likes
Like

Posted 21 April 2013 - 03:15 AM

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?



#10 Paradigm Shifter   Crossbones+   -  Reputation: 5257

Like
0Likes
Like

Posted 21 April 2013 - 03:15 AM

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

#11 Akashi   Members   -  Reputation: 268

Like
0Likes
Like

Posted 21 April 2013 - 03:30 AM

On and off a couple years; my C++ book that I'd been studying out of was actually lost around the time I got to the polymorphism chapter, so yeah, I'm a bit lacking in this area.

 

You make a good point about setting the seed at the creation though.



#12 0r0d   Members   -  Reputation: 815

Like
0Likes
Like

Posted 21 April 2013 - 03:37 AM

On and off a couple years; my C++ book that I'd been studying out of was actually lost around the time I got to the polymorphism chapter, so yeah, I'm a bit lacking in this area.

 

You make a good point about setting the seed at the creation though.

Set the seed at game startup and then dont change it, unless you have a specific need for it... which you probably dont.

 

The simplest solution to what you're trying to do is to have a virtual function that takes in an enum value, the message.  Then in the derived class you can switch on that and call the appropriate function to handle it.  You can send in a second parameter with a pointer to some data if you want.  The simplest way there is to send a void* and case to the right type once you know what the message is.  Not great to do in c++, but simple and will solve your problem.



#13 Paradigm Shifter   Crossbones+   -  Reputation: 5257

Like
0Likes
Like

Posted 21 April 2013 - 03:42 AM

I'm guessing each enemy wants a separate seed so that each enemy does something deterministic independent of the other enemies (so that destroying an enemy won't put the other enemies out of sync due to different numbers of calls to rand() or whatever). That's just a guess though!

 

I wouldn't recommend having an uber virtual function and a message which you switch with, that kind of defeats polymorphism. If you have an enormous number of messages you need to handle, it gets more attractive, though. And void* isn't good advice either for C++.


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

#14 Akashi   Members   -  Reputation: 268

Like
0Likes
Like

Posted 21 April 2013 - 04:17 AM

Set the seed at game startup and then dont change it, unless you have a specific need for it... which you probably dont.

 

When I don't seed the different enemies' RNG values each time they move, they all move in the same directions at each loop iteration. I seed with the index variable so each seed number is different and each of them moves at a different time in a different direction.

 

I don't know what you mean by number of messages, or even what you mean by messages.

 

If I set the seed for each of the enemies, I think that would be good for eliminating the need for the second parameter though. This seems like a pretty dead simple solution.



#15 Paradigm Shifter   Crossbones+   -  Reputation: 5257

Like
0Likes
Like

Posted 21 April 2013 - 04:33 AM

That sounds like you're not using your RNG properly. If you seed it just once at the beginning of the program all the enemies should do different things.

 

Setting the seed explicitly for each enemy has a use, that is deterministic randomised behaviour for each enemy, but I don't think that is what you are after.


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

#16 Dragonsoulj   Crossbones+   -  Reputation: 2090

Like
0Likes
Like

Posted 21 April 2013 - 11:35 AM

It's not necessarily a good design idea, but you could add an enum to your base class that determines what derived class to call:

 

enum EnemySubType
{
  BASE_ENEMY,
  RED_ENEMY,
  BLU_ENEMY,
  GRN_ENEMY
};
 
class ENEMY
{
  protected:
    EnemySubType mySubType;
};

 

When you need to call your derived class functions that don't exist in the base class:

if(myEnemy.mySubType == RED_ENEMY)
{
  ((RED) myEnemy).myDerivedClassFunction();
}
else if......

 

If Else statments or Switch Case would work.

 

I agree that you should potentially redesign your classes/look up polymorphism more, but this could work if you just want to get something running.



#17 incertia   Crossbones+   -  Reputation: 777

Like
0Likes
Like

Posted 21 April 2013 - 12:46 PM

You could try a dynamic_cast<RED>, dynamic_cast<BLU>, etc and do work if the resultant pointer is valid.
what

#18 Satharis   Members   -  Reputation: 949

Like
0Likes
Like

Posted 21 April 2013 - 09:46 PM

Pretty much a problem of using subclassed objects in a list or whatever type of collection. Realistically the code and the compiler don't know if an object is of a particular subclass unless you cast it to it. Paradigm was basically suggesting you include a virtual copy of the method you're trying to call in the base class so it has a definition to work with, though that only works if every subclass has it. I.e. if each color of enemy overrides move and you're calling move or something.

 

Ideally if you're going to use a collection like that you only want to use it for iterating purposes, like calling update on groups of objects even if the objects do different things when update is called on them. I'm not really sure what you're trying to accomplish with the AI and seed thing though.



#19 0r0d   Members   -  Reputation: 815

Like
0Likes
Like

Posted 22 April 2013 - 11:30 AM

Set the seed at game startup and then dont change it, unless you have a specific need for it... which you probably dont.

 

When I don't seed the different enemies' RNG values each time they move, they all move in the same directions at each loop iteration. I seed with the index variable so each seed number is different and each of them moves at a different time in a different direction.

 

I don't know what you mean by number of messages, or even what you mean by messages.

 

If I set the seed for each of the enemies, I think that would be good for eliminating the need for the second parameter though. This seems like a pretty dead simple solution.

It sounds like you're doing something wrong.  If you seed the RNG once at startup, then every number you get after that from the rand() function will be pseudorandom.  In other words, any character that uses it to determine their movement should move in a different direction.  

 

Maybe you can include some code so we can see what you're doing.

 

By "messages" I mean something like this:

enum MESSAGE
{
	MSG_DO_SOMETHING,
	MSG_DO_SOMETHING_ELSE,
};

class ENEMY
{
public:
	virtual void move(void){}
	virtual void handle_message(MESSAGE msg) = 0;
}

class RED : public ENEMY
{
public:
	virtual void move(void);
	virtual void handle_message(MESSAGE msg)
	{
		if(msg == MSG_DO_SOMETHING)
		{
			do_something();
		}
	}
private:
	void do_something(void);
}

class BLUE : public ENEMY
{
public:
	virtual void move(void);
	virtual void handle_message(MESSAGE msg)
	{
		if(msg == MSG_DO_SOMETHING_ELSE)
		{
			do_something_else();
		}
	}
private:
	void do_something_else(void);
}

 

This method has its problems, but it's also an easy thing to implement if you want to call different functionality in your derived class objects through a pointer to their base class.



#20 Bregma   Crossbones+   -  Reputation: 5018

Like
0Likes
Like

Posted 22 April 2013 - 02:04 PM

When I don't seed the different enemies' RNG values each time they move, they all move in the same directions at each loop iteration. I seed with the index variable so each seed number is different and each of them moves at a different time in a different direction.

Sounds like you're creating each Enemy's PRNG with the same seed.  Like some have suggested you could use a since global variable for your PRNG (which is the classic C style), or you could have a PRNG member of your game class and use that to generate a sequence of seeds for each enemy at creation time.


Stephen M. Webb
Professional Free Software Developer




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS