Copy object - keep polymorphism

前端 未结 4 2025
借酒劲吻你
借酒劲吻你 2020-12-06 07:13

The following code tries to copy an object and keep the original type. Unfortunately it does not work (every copied object will become a Super instead of being

相关标签:
4条回答
  • 2020-12-06 07:51

    What you want is generally implemented using an abstract clone method in the base class. Special will typically implement this method by returning new Special(*this).

    Also note that it is considered a best practice to make base classes uncopyable.

    0 讨论(0)
  • 2020-12-06 08:01

    Just for the record, this is in the C++ FAQ:

    http://www.dietmar-kuehl.de/mirror/c++-faq/abcs.html#faq-22.5

    0 讨论(0)
  • 2020-12-06 08:05

    This is what you need :

    class Super
    {
        public:
            Super()
            {
            }
    
            virtual Super* clone() const
            {
                return( new Super(*this) );
            };
    };
    
    
    class Special : public Super
    {
        public:
            Special() : Super()
            {
            };
            Special(const Special& _rhs) : Super(_rhs)
            {
            };
            virtual Special* clone() const
            {
                return( new Special( *this ) );
            };
    };
    
    int main()
    {
        Special a;
        Super &c( a );
        Super *b1 = c.clone();
        Special *b2 = a.clone();
        Super *b3 = a.clone();
    }
    

    One of previous examples has the clone for derived class wrong. The above is correct way of implementing the clone method.

    0 讨论(0)
  • 2020-12-06 08:11

    Try this:

    class Super
    {
    public:
        Super();// regular ctor
        Super(const Super& _rhs); // copy constructor
        virtual Super* clone() const {return(new Super(*this));};
    }; // eo class Super
    
    
    class Special : public Super
    {
    public:
        Special() : Super() {};
        Special(const Special& _rhs) : Super(_rhs){};
        virtual Special* clone() const {return(new Special(*this));};
    }; // eo class Special
    

    Note that we have implemented a clone() function that Special (and any other derivative of Super) overrides to create the correct copy.

    e.g:

    Super* s = new Super();
    Super* s2 = s->clone(); // copy of s
    Special* a = new Special();
    Special* b = a->clone(); // copy of a
    

    EDIT: As other commentator pointed out, *this, not this. That'll teach me to type quickly.

    EDIT2: Another correction.

    EDIT3: I really should not post so quickly when in the middle of work. Modified return-type of Special::clone() for covariant return-types.

    0 讨论(0)
提交回复
热议问题