代码之家  ›  专栏  ›  技术社区  ›  MOnsDaR

复制对象保持多态性

  •  5
  • MOnsDaR  · 技术社区  · 15 年前

    不幸的是,它不起作用(每个复制的对象都将成为 Super 而不是与原来的类相同)。

    copySuper(const Super& givenSuper) 不应该知道 超级的 .

    有可能复制吗?或者我必须改变 copySuper

    #include <string>
    #include <iostream>
    
    class Super
    {
    public:
        Super() {};
        virtual ~Super() {};
    
        virtual std::string toString() const
        {
            return "I'm Super!";
        }
    };
    
    class Special : public Super
    {
    public:
        Special() {};
        virtual ~Special() {};
    
        virtual std::string toString() const
        {
            return "I'm Special!";
        }
    };
    
    Super* copySuper(const Super& givenSuper)
    {
        Super* superCopy( new Super(givenSuper) );
        return superCopy;
    }
    
    int main()
    {
        Special special;
        std::cout << special.toString() << std::endl;
    
        std::cout << "---" << std::endl;
    
        Super* specialCopy = copySuper(special);
        std::cout << specialCopy->toString() << std::endl;
    
        return 0;
    }
    
    //Desired Output:
    // # I'm Special!
    // # ---
    // # I'm Special!
    //
    //Actual Output:
    // # I'm Sepcial!
    // # ---
    // # I'm Super!
    
    4 回复  |  直到 15 年前
        1
  •  13
  •   Moo-Juice    15 年前

    试试这个:

    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
    

    例如:

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

    编辑:正如其他评论员指出的, *this ,不是 this

    编辑2:又一次更正。

    编辑3:我真的不应该在工作中这么快发帖。为协变返回类型修改了Special::clone()的返回类型。

        2
  •  4
  •   BЈовић    15 年前

    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();
    }
    

    前面的一个示例的派生类克隆错误。以上是实现clone方法的正确方法。

        3
  •  3
  •   icecrime    15 年前

    你想要的通常是用抽象的 clone 基类中的方法。Special通常通过返回 new Special(*this) .

    还要注意的是,将基类设置为不可压缩是一种最佳实践。