我有一个IStructure由许多类派生的基类。
其中一些类“引用”其他IStructure类。例如,我的班级class GuiButton : public IStructure有一个成员Textstring(也派生自IStructure)。
现在我只是对此闪闪发光,所以我可以直截了当,所以这对你们中的一些人来说可能看起来很奇怪。我想要一个引用IStructure. 例如:
class GuiButton : public IStructure {
public:
Reference<Textstring> Text;
};
我知道你们中的一些人可能想知道我为什么不这样做Textstring* Text。这是因为其中一些引用是“外部的”。Reference模板类仅保存有关IStructure(即名称等)的信息。其中一些类非常大,实例化整个类以仅使用该Name属性是没有意义的。那有意义吗?
所以现在我的问题:
class Textstring : public IStructure;
我可以Textstring使用我的模板来引用 a:
Reference<Textstring> Text;
现在问题来了:我有一些方法要求我向上转换到“IStructure”,例如:
void RegisterReference(Reference<IStructure> &reference);
所以我不能这样做:
Reference<Textstring> txt("TextName");
RegisterReference(txt); // error
我知道我可以通过不Reference作为模板来解决这个问题,但我真的很想这样做,因为它更容易理解和知道引用的类型。
有什么方法可以做到这一点?
谢谢你的帮助!
-亚历克斯