Original Post
Greetings! I'm trying to add a templated member function to a class of mine, but it's not working the way I expected. Here's the code: I'm trying to use the above code in the following manner: When I step through the template code, the pWidget gets assigned properly. However, as soon as the function returns, pListBox is still NULL. Now, if I modified the code to: With the usage of: That works. However, it's not clean. Can someone point out what's wrong here? Using MSVC 2005 Express.
class GUI_Register
{
public:
// Constructors and Destructors
GUI_Register(GUI* pGUI){ m_pGUI = pGUI; }
~GUI_Register(){}
template <typename T>
void GetWidget(const std::string& name, T pWidget)
{
// Initialize the widget to NULL.
pWidget = NULL;
// Search the registry for a widget with a matching name.
for (unsigned int i = 0; i < m_Registry.size(); i++)
{
if (m_Registry.Name == name)
{
// Assign the matching widget.
pWidget = dynamic_cast<T>(m_pGUI->GetWidget(m_Registry.WidgetLocation));
break;
}
}
}
private:
GUI* m_pGUI;
};
GUI_ListBox* pListBox = NULL;
m_pRegister->GetWidget("List Box 1", pListBox);
// Check for NULL to make sure the widget found is of the correct type, etc.
template <typename T>
T GetWidget(const std::string& name, T pWidget)
{
// Initialize the widget to NULL.
pWidget = NULL;
// Search the registry for a widget with a matching name.
for (unsigned int i = 0; i < m_Registry.size(); i++)
{
if (m_Registry.Name == name)
{
// Assign the matching widget.
pWidget = dynamic_cast<T>(m_pGUI->GetWidget(m_Registry.WidgetLocation));
break;
}
}
return pWidget;
}
pListBox = m_pRegister->GetWidget("List Box 1", pListBox);