Hi all, the setup is as follows:

-One base class

-My top layer class that holds:
-Four derived classes from said base class(four different types of objects that all derived from my base class)

Is it possible to have a function in my top layer class that has one object parameter that will accept either the base class or any of the four derived classes?

I have a top layer class that I am trying to handle everything in, and writing four different functions for each derived class to get the shared functionality between all 4 classes seems way overboard.

Thanks!

Can you have void doSomething(BaseClass& b){...} in your toplayer?
Maybe you should show some code of exactly what you are trying to do.

I have my base class here which defines so far the common attributes between all classes. Derived classes will have there own maxhealth, currenthealth, jump count and so on and so forth. My top layer is my Game Manager, which has a function called AdjustHealth, which depending on the object pointer passed into it, will tell a fighter to SetCurrentHealth.

I dont want to have an AdjustFighterOneHealth, AdjustFighterTwoHealth etc...
I want to say

AdjustHealth(Fighter *f, take health, 20)
{
f->SetCurrentHealth(take health, 20);
}

my game manager holds object pointers to each of the classes.

class FighterBase
{
	public:

		FighterBase();

		float GetMaxHealth();
		float GetCurrentHealth();
		void SetCurrentHealth(int adjustment, float value);

		FighterCoordinates GetCoordinates();

	private:

	protected:

		float _maxHealth;

		float _currentHealth;

		float _moveSpeed;

		int _jumpCount;

		float _durability;

		FighterCoordinates _coords;

		int _id;
		
};

class FighterOne : public FighterBase
{
	public:

		FighterOne();

	private:

};

class FighterTwo : public FighterBase
{
	public:

		FighterTwo();

	private:

};

class FighterThree : public FighterBase
{
	public:

		FighterThree();

	private:

};

class FighterFour : public FighterBase
{
	public:

		FighterFour();

	private:

};

#endif
This article has been dead for over six months. Start a new discussion instead.