Are templates the best option? If so, is this the correct syntax?

Hello!

So I have this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    //base class
    class InterfaceItem
    {
    public:
        //iterates through m_SubItems vector looking for an item with that name and
        //returns it
        InterfaceItem* GetSubItem(string Name);
    
    private:
        vector<InterfaceItem*> m_SubItems;
    }
    
    //one of the classes that derive from InterfaceItem
    class Window : public InterfaceItem
    {
        //other functions
    }

So if I do something like
1
2
    Window ThisWindow; //pretend it is already initialized and has sub-items
    ThisWindow.GetSubItems();

it will return an object of type InterfaceItem*, so I am not able to access any of the Window specific functions unless I do something like
 
    Window* TempWindow = static_cast<Window*>(ThisWindow.GetSubItems());


What is the best solution for this? Is it to use a function template? If so, would this be the correct syntax?

1
2
3
4
5
6
7
8
    class InterfaceItem
    {
    public:
        template<class Type*> Type* GetSubItem(string Name);
        
    private:
        vector<InterfaceItem*> m_SubItems;
    }


I've tried this and I got some WEIRD errors. Files that pretty had nothing to do with this started saying classes that were clearly #included didn't exist and some other weird stuff
In the end I managed to use template functions. I found out the reason I was getting those absurdly strange errors was because I was using this syntax <class Type*>, where the "*" makes no sense. I just removed that part and it compiled correctly, after I changed the call I wanted to "(...)->template GetSub<Window>("name to look for").(rest of the code)".
Topic archived. No new replies allowed.