The Basic question is does anyone know a method for this in c++? Class 2 is subclass of Class1 and it has to stay that way.
Class1* a;
Class2* b = (Class2*)a;
basically allowing you
Class1* a;
Class2* b = a;
// tried this but didn't seem to work
class Class1
{
operator Class2*() { return (Class2*)this; }
}
I couldn't really find anything sorry if that's a retarded question / duplicate but didn't quite get my head around this. Would appreciate if someoen had similar stuff.
Edit: some extra Information Class1 = baseclass e.g Player Class2 = subclass e.g Weapon
Class1 holds a pointer to the Subclass which has same values like origin
Player has a Pointer to Weapon basically what I want to do is
GetEntity returns a Pointer of Type Player (but the return value could also be Weapon)
Basically now what I want to do is automate the process of casting it like
Player* p = GetEntity(0);
Player* p2 = GetEntity(1);
Weapon wpn = (Weapon*)p2;
//so that you are able to do
Weapon wpn = p2;
// Also that Stuff like that would be possible
void Test(Weapon *wpn);
Player *player = GetEntity(1);
// Yes I know this works Test((Weapon*)player) but I am lazy and that's the goal
Test(player);
without casting it.
Class1
inherit fromClass2
... Decide which is-a relationship your are after. – StoryTeller - Unslander Monica Dec 18 '17 at 10:45