Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ deep copying with objects

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

Now given the object b class function as follows:

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

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

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

I have a few resulting questions.

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

int ObjectBDataMember; 

and not as

int *ObjectBDataMember;

why is the data member accessed as

this->ObjectBDataMember

and not as

this.ObjectBDataMember

?

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.

like image 819
Andy K. Avatar asked Jan 16 '13 17:01

Andy K.


People also ask

Does object spread deep copy?

The spread operator makes deep copies of data if the data is not nested. When you have nested data in an array or object the spread operator will create a deep copy of the top most data and a shallow copy of the nested data.

What is a deep copy C?

A deep copy, in contrast, means that you copy an entire object (struct). If it has members that can be copied shallow or deep, you also make a deep copy of them.

How do you deep copy an object in AC?

Copy an Object With Object.assign() was the most popular way to deep copy an object. Object. assign() will copy everything into the new object, including any functions. Mutating the copied object also doesn't affect the original object.


2 Answers

  1. In C++, this is defined as being a (non-modifiable) pointer to the current object. For that reason, you use this->aMember to access aMember. This is independent of the type that aMember has. (Note: Using this->aMember is equivalent to just using aMember as long as there are no local variables or function parameters using that same name).

  2. Because ObjectBDataMember is an int, copying it is not referred to as either shallow or deep. Those concepts are only used in the context of copying pointers.

For example:

ObjectBType* b1 = new ObjectBType();

ObjectBType* b2 = b1; // shallow copy. b1 and b2 refer to the same object.
ObjectBType* b3 = new ObjectBType(*b1); /* deep copy. b1 and b3 refer to 
  different objects that happen to have the same value. */
like image 143
Bart van Ingen Schenau Avatar answered Oct 05 '22 00:10

Bart van Ingen Schenau


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

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.

"Is this a deep or shallow copy?"

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.

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.

like image 23
Andy Prowl Avatar answered Oct 04 '22 22:10

Andy Prowl