用对象进行C ++深度复制 [英] C++ deep copying with objects

查看:44
本文介绍了用对象进行C ++深度复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好.我在理解共享项目中的C ++中的对象进行深度和浅层复制背后的逻辑时遇到了麻烦,因此我创建了以下示例.

Good morning. I am having trouble understanding the logic behind deep and shallow copying with objects in C++ in a shared project, so I have created the following example.

int main() {
    ObjectAType* objecta = ObjectAType::New();
    ObjectBType* objectb = ObjectBType::New();

    // some operation to populate data members of object a
    objecta->Operation();

    // assume I have accessors to return datamembers of object a
    // I wish to make a deep copy of SOME of those data members into object b
    objectb->AlignWithA(objecta);

    objecta->Delete();
    objectb->Delete();

    return 0;
}

现在给对象b类的功能如下:

Now given the object b class function as follows:

public:
void ObjectBType::AlignWithA(ObjectAType* objecta) {
    this->ObjectBDataMember = objecta->DataAccessor();
}
protected:
int ObjectBDataMember;

数据访问器在def类中就是这样的

And the data accessor is just something like this within the class def,

public:
int ObjectAType::DataAccessor() {
    return this->ObjectADataMember;
}
protected:
int ObjectADataMember;

我有一些问题要问.

1)由于在对象b中,数据成员被声明为

1) Since in object b, the data member is declared as

int ObjectBDataMember; 

而不是

int *ObjectBDataMember;

为什么数据成员以

this->ObjectBDataMember

而不是

this.ObjectBDataMember

?

2)这是深拷贝还是浅拷贝?

2) Is this a deep or shallow copy?

如果我遗漏了重要的内容,我深表歉意.我不是一个程序员,所以像这样的事情很容易使我感到困惑.文献使我更加困惑.谢谢您的时间.

I apologize if I have left out important bits. I'm not much of a programmer so things like this easily confuse me. The literature has just confused me further. Thank you for your time.

推荐答案

为什么数据成员以 this-> ObjectBDataMember 而不是以 this.ObjectBDataMember ?"

"Why is the data member accessed as this->ObjectBDataMember and not as this.ObjectBDataMember?"

这是因为 this 是一个指针,并且-> 运算符跟随之前的指针来访问该成员之后.

That's because this is a pointer, and the -> operator follows the pointer that comes before it to access the member that comes after it.

这是深拷贝还是浅拷贝?"

如果您是指整数变量的副本,则可以将其称为浅表副本,但是由于 int 不是数据结构,因此无需对其进行限定.

If you mean the copy of the integer variable, you can call that a shallow copy, but there's no need to qualify it as such because int is not a data structure.

术语"深层复制"是指与要复制的对象相关联的所有对象的递归复制:如果数据结构为 S 包含指针的成员变量,这些变量将 S 的实例(例如, s1 )深度复制到另一个 S (例如, s2 )表示以递归方式复制 s1 变量指向的每个对象,以便将 s2 副本关联这些对象,而不是与 s1 关联的 same 对象(浅复制就是这种情况).

The term "deep copy" refers to a recursive copying of all objects associated to the object being copied: if a data structure S contains member variables which are pointers, deep copying an instance of S (say, s1) into another instance of S (say, s2) means recursively copying each object pointed by variables of s1 so that s2 will be associated to copies of those objects, rather than to the same objects to which s1 is associated (that would be the case for shallow copying).

在这里,您没有任何指针成员变量,因此在这种情况下,深层"与浅层"副本的概念就失去了意义.

Here, you do not have any pointer member variables, so the concept of "deep" vs "shallow" copy loses its meaning in this context.

这篇关于用对象进行C ++深度复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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