Archived

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

copy template pointers

This topic is 5114 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, 
   {
    protected:
     std::vector<GUIComponent*> children;
    public:
     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); }

etc.

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

Share this post


Link to post
Share on other sites