danijar
danijar

Reputation: 34185

Is it possible to Cast an Object to a not related Class?

Say I have two classes with different names but the exactly same structure. It there a way to cast an object of the one class to one of the other?

This might sound stupid to do but there is a reason why I want to do that. The architecture of my application provides the abstract classes component and storage The ready application will contain several specialized components derived from component and each of them will define its own storage type, derived from storage. During initialization of the application, for each component there will be a storage object of its custom storage type created and passed as pointer to the component.

This way all component stay completely independent which is great for re-usability and testing. But, of course, there is a need to exchange informations between components. To do that with independence in mind, I want to let two components get a storage pointer pointing to the same storage. The pointer by constraint must be of the component specific storage type.

framework code (abstract classes)

class Storage {};

class Component {
public:
    void SetStorage(Storage* storage);
private:
    Storage* storage;
};

example component

class PhysicsStorage : public Storage;
class PhysicsComponent : public Component;

another component

class CollisionStorage : public Storage; // same structure as PhysicsStorage as both components need the same data like world coordinates and rotations of all forms in the 3d space
class CollisionComponent : public Component;

main application

#include "system.h"

PhysicsStorage Worlddata;

PhysicsComponent Physics;
CollisionComponent Collision;

Physics.SetStorage(&Worlddata);
Collision.SetStorage(&Worlddata); // this points to a PhysicsStorage but that is actually the same like a CollisionStorage which is expected

So I wonder if there is a way of casting the pointer of say PhysicsStorage* to CollisionStorage*. In this case, both are defined in the file of their related component class. And both are derived from abstract Storage.

I only want to do that in the case that both custom storage types have exactly the same structure. Otherwise it would be senseless. I am not so familiar with advances pointer usage so I wonder if there is a way to do that. What I know is that you can cast to a base class, but this isn't what I want here. Using a shared storage type for two components would break independence. Thanks a lot!

Upvotes: 0

Views: 243

Answers (1)

cdhowie
cdhowie

Reputation: 169038

This may work, and it may not. I believe this is a case of undefined behavior, and I would avoid doing this in production code.

Consider instead pushing the common fields and methods up into a new class and having ComponentOneStorage and ComponentTwoStorage inherit that class. Then you can pass around pointers/references to the base class instead, which will give you access to the common data without having to cast between incompatible pointer types.

Upvotes: 2

Related Questions