创建不与虚拟基类一起使用的对象的克隆 [英] Creating clone of an object not working with virtual base class

查看:142
本文介绍了创建不与虚拟基类一起使用的对象的克隆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 #include< iostream> 
using namespace std;

类东西
{
public:
int j;
Something():j(20){cout<Something initialized。j =<< j<< endl;}
};

class Base
{
private:
Base(const Base&){}
public:
Base(){}
virtual Base * clone(){return new Base(* this); }
virtual void ID(){cout<<BASE<< endl; }
};

类派生:public Base
{
private:
int id;
Something * s;
Derived(const Derived&){}
public:
Derived():id(10){cout<<Called constructor and assigned id<< endl; s = new Something();}
〜Derived(){delete s;}
virtual Base * clone(){return new Derived(* this); }
virtual void ID(){cout<<DERIVED id =<< id<< endl; }
void assignID(int i){id = i;}
};


int main()
{
Base * b = new Derived();
b-> ID();
Base * c = b-> clone();
c-> ID();
} // main

运行时:

 调用的构造函数和分配的ID 
初始化的东西。 j = 20
DERIVED id = 10
DERIVED id = 0



在第一个链接中,Space_C0wb0y表示


因为克隆方法是
的方法,实际的对象的类,它可以
也创建一个深度副本,它可以访问
所有类的成员
to,所以没有问题。


我不明白深拷贝可能发生。在上面的程序中,甚至不发生浅拷贝。 我需要它来工作,即使Base类是一个抽象类。我如何在这里做一个深拷贝?帮助?

好吧,你的复制构造函数什么也不做,所以你的克隆方法不会复制。 p>

查看行 Derived(const Derived&){}



编辑:如果你添加代码来复制Derived的所有成员,它将变成浅拷贝。如果你也复制(通过制作一个新的实例)你的Something的实例,它将成为一个深层副本。


#include<iostream>
using namespace std;

class Something
{
  public:
  int j;
  Something():j(20) {cout<<"Something initialized. j="<<j<<endl;}
};

class Base
{
  private:
    Base(const Base&) {}
  public:
    Base() {}
    virtual Base *clone() { return new Base(*this); }
    virtual void ID() { cout<<"BASE"<<endl; }
};

class Derived : public Base
{
  private:
    int id;
    Something *s;
    Derived(const Derived&) {}
  public:
    Derived():id(10) {cout<<"Called constructor and allocated id"<<endl;s=new Something();}
    ~Derived() {delete s;}
    virtual Base *clone() { return new Derived(*this); }
    virtual void ID() { cout<<"DERIVED id="<<id<<endl; }
    void assignID(int i) {id=i;}
};


int main()
{
        Base* b=new Derived();
        b->ID();
        Base* c=b->clone();
        c->ID();
}//main

On running:

Called constructor and allocated id
Something initialized. j=20
DERIVED id=10
DERIVED id=0

My question is related to this, this and this post.

In the first link, Space_C0wb0y says

"Since the clone-method is a method of the actual class of the object, it can also create a deep-copy. It can access all members of the class it belongs to, so no problems there."

I don't understand how a deep copy can happen. In the program above, not even a shallow copy is happening. I need it to work even if the Base class is an abstract class. How can I do a deep copy here? Help please?

解决方案

Well, your copy constructor does nothing, so your clone method does nothing in the way of copying.

See line Derived(const Derived&) {}

EDIT: if you add code to copy by assignment all members of Derived, it will become a shallow copy. If you also copy (by making a new instance) your instance of Something, it will become a deep copy.

这篇关于创建不与虚拟基类一起使用的对象的克隆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆