vector list problem

Started by
2 comments, last by Zeblar Nagrim 22 years, 2 months ago
Hello, I have problems with the std::vector class:
  
CreateWnd()
{

...

CGUIWnd wnd = CGUIWnd(idwnd,x,y,w,h);
m_aWnds.push_back(wnd);

CGUIButton button = CGUIButton(idbutton,m_aWnds.back(),x,y,w,h);
m_aButtons.push_back( button );

...
}
  
What happens? I create a window and than a button. The window are registered as the parent for the button. CGUIButton is derived from CGUIWnd. In the CGUIButton class I have a pointer to a CGUIWnd (that is the parent of the button). This works fine first time the function is called but when the second window is created the first button (the button on the window) get crap values! Something happens when the vector object m_aButtons inserts a new element to the list. I think it is because of the CGUIWnd pointer in the CGUIButton class. When a new element is inserted in m_aWnds the pointer in CGUIButton class isn´t valid anymore. Please help! Zeblar Nagrim, Lord of Chaos
Advertisement
With the limitted code you''ve put here, it appears that your problem is in the definition of your vector. It looks like you''re defining a vector of objects, not pointers, and pushing-in derived class objects. This will not work. In order to use polymorphism in STL containers you have to use pointers (or smart pointers), not objects.
Thanks for your advise!

You want me to change from,
   std::vector<CGUIButton> m_aButtons;   
to
   std::vector<CGUIButton*> m_aButtons;   
?

Edited by - Zeblar Nagrim on January 30, 2002 5:39:21 PM
Thank you,

works fine now!

Zeblar Nagrim, Lord of Chaos

This topic is closed to new replies.

Advertisement