I have a base class IStructure which is derived by many classes.
Some of these classes ‘reference’ other IStructure classes. For example, my class class GuiButton : public IStructure has a member of Textstring (which derives from IStructure, as well).
Now I’m just glistening over this so I can get to the point, so this may seem weird to some of you. I would like to have a template class ‘Reference’ that references an IStructure. For example:
class GuiButton : public IStructure {
public:
Reference<Textstring> Text;
};
I know some of you might be wondering why I just don’t do Textstring* Text. This is because some of these references are “external”. The Reference template class only holds information about the IStructure (ie. Name, etc.). Some of these classes are insanely big and it would be pointless to instantiate that whole class to only use the Name property and what not. Does that make sense?
So now to my question:
class Textstring : public IStructure;
I can reference a Textstring by using my template:
Reference<Textstring> Text;
Now here’s the issue: Some methods I have require me to upcast to `IStructure’, for example:
void RegisterReference(Reference<IStructure> &reference);
So I couldn’t do this:
Reference<Textstring> txt("TextName");
RegisterReference(txt); // error
I know I can cure this by not having Reference be a template, but I would really like to because it makes it easier to understand and know what type the reference is.
What are some ways I can accomplish this?
Thanks for your help!
-Alex
You can make your function a template on the type parameter of the
Reference, like this: