基本问题是否有人知道在C++中的这种方法?第2类是Class1的子类,它必须保持这种状态。C++指针强制重载
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; }
}
我真的无法找到任何东西,对不起,如果这是一个弱智的问题/复制,但并没有完全让我解决这个头。如果someoen有类似的东西,将不胜感激。
编辑:一些额外的信息 的Class1 =基类如球员 类2 =子类如武器
的Class1具有指向具有类似起源相同的价值观
球员有一个指针指向武器 子类基本我想要做的就是
GetEntity返回类型的球员的指针(但返回值也可以是武器)
基本上现在就是我想要做的就是像自动化
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);
铸造它的过程,而不铸造它。
公有继承...... – StoryTeller
即使我在Class1的Class2中插入数据,我仍然需要投射我试图自动化的指针。 – Shizo
不,你让'Class1'继承自'Class2' ...决定你的后面是哪一个* -a *关系。 – StoryTeller