Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


copy template pointers

This topic is 5450 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi i have a std::vector, which gets loaded with pointers on GUIComponents:
  class Container : public GUIComponent, 
     std::vector<GUIComponent*> children;
     template<class T> unsigned int add(T *child)
       T *copy = new T(*child);
       unsigned int ret = children.size();
       children.push_back( (GUIComponent*)copy );
       return ret;
in the destructor, i delete that pointers. But if i want to add the container into another container, i need to copy the Components children''s values are pointing, since those might be deleted by the destructor of the original container. how do i copy data if i don''t know the datatype?

Share this post

Link to post
Share on other sites
I'd do it by adding a virtual function named clone.

GUIComponent* Button::clone() const { return new Button(*this); }
GUIComponent* Textbox::clone() const { return new Textbox(*this); }


[edited by - Beer Hunter on December 14, 2003 4:21:14 AM]

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!