Question

Hello I am having a small issue with storing derived type objects within a 2-dimensional array of the base type without loosing the derived type stored in the array.

For example there is the following Base and Derived class:

class Base{

}

class Derived: public Base{

}

There is a point where I have a Base object created like the following:

Base objectB;

Then I cast the above object to the type of the Derived class like the following:

Base *referencePointer = &objectB;
Derived *derivedPointer = static_cast<Derived*>(referencePointer);

At this point everything is working correctly (if I print out the type of derivedPointer it is of type Derived).

Now I have a 2 dimensional array of type Base class initialized as the following:

Base *baseArray[5][5];

Now I input the derivedPointer value into the array as the following:

baseArray[x][y] = derivedPointer;

This is where the problem occurs as it stores in the referencePointer but it becomes of Base type (object slicing) and I am unsure as to how I would go about storing the referencePointer value and keep its type within an array of type Base.

Any help is much appreciated,

Thank you!

Was it helpful?

Solution

video game with different types of players that are able to move different based on there type but they all have certain base functionality that are the same.

Imagine that the 'certain base functionality' is implemented in the base class. These methods can easily be made available to any derived classes as public or protected methods. Thus eliminating duplicate code.

Now imagine the base class provides a virtual method "void move(CoordinatesXYZ_t to, VelocityXYZ_t vel, AccelerationXYZ_t acc)".

Simply by adding a (virtual) method with the same signature in a derived class, each derived object can provide their own unique response to the gaming engine's move command. There is no need for the gaming engine to know what type of derived object it is, the correct virtual method will be invoked.

Downcast'ing is simply not needed. The game engine can be written so that it simply does not need to care what kind of derived class it is dealing with - as long as the derived object conforms to the interface. The game engine invokes a virtual method, which will result in the invocation of the appropriate derived object method.

The actual method can be unique for every member of the 2 dimensional array of base pointers.

The actual method can even do nothing (i.e. a tree probably does not move.)

And the derived class does not have to provide a move method unless the desired behaviour is different from what the base class' move method provides.

OTHER TIPS

Derived *derivedPointer = static_cast<Derived*>(referencePointer);

This is a mistake because referencePointer actually points to a Base object. The compiler won't magically tack on extra bits to the Base object to turn it into a Derived object. To avoid this error, use dynamic_cast instead of static_cast. Then you will be able to detect failure.

Note, Base needs to be polymorphic for this to work, it must have at least 1 virtual function.

By the way, this is nothing to do with slicing. Even if referencePointer pointed to a Derived, that's fine: you have a 2-D array of pointers, it's fine to put in a Base *, which might point to an object of Derived type. Slicing would be if you had a 2-D array of Base.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top