克隆类的实例,然后克隆克隆,然后克隆克隆...?

时间:2019-03-13 11:22:38

标签: c++ c++11

我需要从另一个类的实例中克隆一个类,让克隆体继承原始类的特征,并定义一些新的/更改的属性,然后克隆克隆体,克隆克隆体等。希望下面的代码节目想要我想要实现。我已经尝试了一些有关克隆类的stackoverflow答案的变体,但是我无法使其正常工作。有人可以帮忙吗?

这是(不完整的)代码:

#include <cstdio>

class BASE {

    private:

        int x = 1;
        int y = 2;
        int z = 3;

    public:

        void id() { printf("BASE\n"); }

        void f1() { printf("BASE f1\n" }
        void f2() { printf("BASE f2\n" }
        void f3() { printf("BASE f3\n" }

        void x() { printf("x = %d\n", x);
        void y() { printf("y = %d\n", y);
        void z() { printf("z = %d\n", z);

        void x(int v) { x = v);
        void y(int v) { y = v);
        void z(int v) { z = v);
}

class A {

    public:

        void id() { printf("A\n"); }

        void f1() { printf("A f1\n" }

        void x() { printf("x = %d\n", x);
        void y() { printf("y = %d\n", y);
        void z() { printf("z = %d\n", z);

        void x(int v) { x = v);
        void y(int v) { y = v);
        void z(int v) { z = v);
}

class B {

    public:

        void id() { printf("B\n"); }

        void f1() { printf("B f1\n" }

        void x() { printf("x = %d\n", x);
        void y() { printf("y = %d\n", y);
        void z() { printf("z = %d\n", z);

        void x(int v) { x = v);
        void y(int v) { y = v);
        void z(int v) { z = v);
}

class C {

    public:

        void id() { printf("C\n"); }

        void f1() { printf("C f1\n" }

        void x() { printf("x = %d\n", x);
        void y() { printf("y = %d\n", y);
        void z() { printf("z = %d\n", z);

        void x(int v) { x = v);
        void y(int v) { y = v);
        void z(int v) { z = v);
}

int main() {

    BASE *ptr = new BASE();

    ptr->id();
    ptr->f1();
    ptr->f2();
    ptr->f3();
    ptr->x();
    ptr->y();
    ptr->z();

    printf("\n\n");

    A *a = base->clone();

    delete ptr;
    A *ptr = a;

    ptr->id();
    ptr->f1();
    ptr->f2();
    ptr->f3();
    ptr->x(7);  // change member x to 7
    ptr->x();
    ptr->y();
    ptr->z();

    printf("\n\n");

    B *b = a->clone();

    delete ptr;
    B *ptr = b;

    ptr->id();
    ptr->f1();
    ptr->f2();
    ptr->f3();
    ptr->x();
    ptr->y(9);  // change member y to 9
    ptr->y();
    ptr->z();

    printf("\n\n");

    C *c = b->clone();

    delete ptr;
    C *ptr = c;

    ptr->id();
    ptr->f1();
    ptr->f2();
    ptr->f3();
    ptr->x();
    ptr->y();
    ptr->z(5);  // change member z to 5
    ptr->z();

}

代码不完整-最值得注意的是未编写“ clone()”函数。那就是我不知道该怎么做的地方。

运行程序应产生输出:

BASE
BASE f1
BASE f2
BASE f3
x = 1
y = 2
z = 3


A
A f1
BASE f2
BASE f3
x = 7
y = 2
z = 3


B
B f1
BASE f2
BASE f3
x = 7
y = 9
z = 3


C
C f1
BASE f2
BASE f3
x = 7
y = 9
z = 5

这可以实现吗?如果可以,怎么办?

1 个答案:

答案 0 :(得分:0)

听起来您希望ABCBASE继承并可以从中构造。

#include <cstdio>

class BASE {
private:

    int m_x = 1;
    int m_y = 2;
    int m_z = 3;

public:

    virtual void id() { printf("BASE\n"); }

    virtual void f1() { printf("BASE f1\n"); }
    void f2() { printf("BASE f2\n"); } /* virtual ? */ 
    void f3() { printf("BASE f3\n"); } /* virtual ? */ 

    void x() { printf("x = %d\n", m_x); }
    void y() { printf("y = %d\n", m_y); }
    void z() { printf("z = %d\n", m_z); }

    void x(int v) { m_x = v; }
    void y(int v) { m_y = v; }
    void z(int v) { m_z = v; }
};

class A : public BASE {
public:
    A(const BASE & base) : BASE(base) {}
    A& operator = (const BASE & base) { static_cast<BASE&>(*this) = base; return *this; }

    void id() override { printf("A\n"); };
    void f1() override { printf("A f1\n"); };
};

class B : public BASE {
public:
    B(const BASE & base) : BASE(base) {}
    B& operator = (const BASE & base) { static_cast<BASE&>(*this) = base; return *this; }

    void id() override { printf("B\n"); };
    void f1() override { printf("B f1\n"); };
};

class C : public BASE {
public:
    C(const BASE & base) : BASE(base) {}
    C& operator = (const BASE & base) { static_cast<BASE&>(*this) = base; return *this; }
    void id() override { printf("C\n"); };
    void f1() override { printf("C f1\n"); };
};

int main() {

    BASE base;

    base.id();
    base.f1();
    base.f2();
    base.f3();
    base.x();
    base.y();
    base.z();

    printf("\n\n");

    A a = base;

    a.id();
    a.f1();
    a.f2();
    a.f3();
    a.x(7);  // change member x to 7
    a.x();
    a.y();
    a.z();

    printf("\n\n");

    B b = a;

    b.id();
    b.f1();
    b.f2();
    b.f3();
    b.x();
    b.y(9);  // change member y to 9
    b.y();
    b.z();

    printf("\n\n");

    C c = b;

    c.id();
    c.f1();
    c.f2();
    c.f3();
    c.x();
    c.y();
    c.z(5);  // change member z to 5
    c.z();

}

您实际上不需要继承,因为BASEABC之间只有数据差异。