SharkBytes
SharkBytes

Reputation: 211

C++ Runtime class switching, relying on base class inheritance

I am not fully versed in C++ and with the current project I am working on, have hit a small snag. I have come across this: http://www.terrainformatica.com/2010/08/cpp-how-to-change-class-of-object-in-runtime/ but I am unsure if this is the solution I am looking for or if there are any better alternatives.

My intentions is to switch turret types stored in memory, despite them being different instantiations of base CTurret. And with as little performance impact as possible since this is going to be implemented in a simple game.

Basically I have a base class CTurret of base class CEntity. Now I have several Turrets (Basic, Fast, Harmless). Each turret uses the base class CTurret I want to reserve some memory to hold generic turrets which can then be simply swapped out for an actual turret type. Better visual below:

class CEntity...
class CTurret : public CEntity...
class CBasicTurret : public CTurret...
class CFastTurret : public CTurret...

Memory (5 generic turrets)
Array[CTurret, CTurret, CTurret, CTurret, CTurret] // Can not use generic!

User wants a basic turret, populate an available generic turret:
Array[CBasicTurret, CTurret, CTurret, CTurret, CTurret] // Ah, a basic turret I'll use that.

When no longer needed:
Array[CTurret, CTurret, CTurret, CTurret, CTurret] // Back to original.

So far I can see only two options to achieve what I want:
1) I could put all the turret types in the basic class and change it form the base to... an actual class. But this would get messy fast.
2) I could reserve memory for each turret type which would not be an ideal situation since memory is precious and should not be wasted as such. Also the user may request more than is already reserve which could pose future problems.

Upvotes: 0

Views: 372

Answers (1)

Rob
Rob

Reputation: 1974

You need to decide what the difference is between turret types.

The general choices are a polymorphic base class (with the derived classes overriding inherited virtual functions to specialise behaviour) or a single class with state (e.g. an enumerated type to describe operating modes, a float to describe speed, etc).

An array of smart pointers can be used if you decide on a polymorphic base. Simply pick which pointer (or index into the array) to use. The pointers (and objects they point to) will be in memory anyway.

With a single object, just change its state as required. So change values like height, width, operating mode, etc. This is the better option if you're really worried about memory usage (only one object in memory needed, although there is nothing stopping you having an array of objects, where each element has different state).

The placement new trick you linked to has a few gotchas that have been glossed over, and the particular example of morphing an object with data into an object that doesn't is problematical (e.g. causing the caller to exhibit undefined behaviour).

Upvotes: 1

Related Questions