Question

I am working on a C++ program, but I am having problem with multiple inheritance when using cloning. The problem (in a simplified form) is the following.

I want to be able to clone all objects derived from the class Base.

class Base{
public:

    virtual Base* clone()const=0;
};

I want to define two other classes derived from Base, which are both abstract, i.e. I cannot define the clone function, but I have to declare them in some way (I want to make sure, that if I clone Derived*, I will get back Derived* and not Base*, i.e. I want to avoid casting at the point of application)

class Derived1: public virtual Base{
public:
    virtual Derived1* clone()const=0;
};

class Derived2: public virtual Base{
public:
    virtual Derived2* clone()const=0;
};

The problem comes, when I declare a fourth class, which is inherited from both Derived1 and Derived2:

class Derived3: public Derived1,public Derived2{
protected:
    int b;
public:
    Derived3():b(3){};
    Derived3(Derived3 const& l_B) {b=l_B.b;};
    virtual Derived3* clone()const{return new Derived3(*this);}
    ;
};

In this case I will get from Visual C++ 2010 compiler C2250: 'Derived3' : ambiguous inheritance of 'Derived1 *Base::clone(void) const'. If I declare clone() in Derived1 and Derived2 not pure virtual, but without definition, the error remains the same:

class Base{
public:

    virtual Base* clone()const=0;
};

class Derived1: public virtual Base{
public:
    virtual Derived1* clone()const;
};



class Derived2: public virtual Base{
public:
    virtual Derived2* clone()const;
};

class Derived3: public Derived1,public Derived2{
protected:
    int b;
public:
    Derived3():b(3){};
    Derived3(Derived3 const& l_B) {b=l_B.b;};
    virtual Derived3* clone()const{return new Derived3(*this);}
    ;
};

Using virtual inheritance at Derived3 will not help either, and I cannot find a way to solve it, I simply run out of ideas. It is very important that all classes should return a pointer of the same type, e.g. I want to do later:

Derived3 test;
Derived1* test2=&test;
Derived1* test3=test2->clone();

and I want to avoid:

Derived3 test;
Derived1* test2=&test;
Derived1* test3=dynamic_cast<Derived1*>(test2->clone());

If anyone has an idea or solution, I would appreciate it!

Was it helpful?

Solution

The idea would be to have a protected virtual method clone_impl in Base and a public non-virtual clone:

class Base
{
protected:
    virtual Base* clone_impl() const = 0;

public:
    Base* clone() const
    {
      return clone_impl();
    }
};

and for each derived class provide clone_impl when the class is not abstract and a non-virtual wrapper clone for all derived classes:

class DerivedX : ...
{
protected:
    // only when not abstract:
    virtual Base* clone_impl() const
    {
      return new DerivedX(*this);
    }

public:
    // in each derived class
    DerivedX* clone() const
    {
      return static_cast<DerivedX*>(clone_impl());
    }
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top