Reputation: 15
I have a class called Directory
with certain members followed by a copy constructor.
class Directory{
private:
char * Name;
int Telephone_Number;
char * Address;
public:
Directory (Directory & b)
{
Name = new char [10]; //Just assume that the name will not be greater than //10 characters
Address = new char [30]; //Same here
strcpy (Name, b.Name);
Telephone_Number = b.Telephone_Number;
strcpy (Address, b.Address);
}
};
I wanted to know if my copy constructor would perform deep copy or shallow copy. I understand that it is deep copying Address
and Name
because new memory is being allocated for them, but what about Telephone_Number
?
Is my code doing shallow copying or deep copying? Could anyone explain copy constructors in general to me?
Upvotes: 1
Views: 9733
Reputation: 177
Name = new char [10];
Address = new char [30]; //Same here
strcpy (Name, b.Name);
strcpy (Address, b.Address);
Name and Address will just have the memory address to the actual string. So when we copy 'Name'/'Address' we have to copy the actual content which is pointed by 'Name'/'Address'.If we do just shallow copy as below,
b.Name = Name;
b.Address = Address;
we are just pointing to the data which was created by someone which may also get deleted by that someone.If someone delete the actual content,we will be pointing the content assuming its available.Now the pointer is dangling(Pointing Nothing). This is why we use copy constructor.
Telephone_Number = b.Telephone_Number;
'Telephone_Number' is holding the actual value.So shallow copy is enough to copy the value
Name/Address will have the memory address and shallow copy will copy just the memory address of the actual value.
Telephone_Number will have a integer value and shallow copy will copy that integer value
So the copy constructor is 'PERFECT' doing a deep copy.
Upvotes: 0
Reputation: 20383
Obviously it is deep copying Address and Name because new memory is being allocated for them, but what about Telephone_Number?
An int, like the telephone number, is always deep copied.
Is my current code doing shallow copy or deep?
Deep
Could anyone explain copy constructors in general and copy constructors specifically to me?
A compiler-synthesized copy constructor would just generate member by member copy, i.e. a pointer value is copied, not its contents. Compiler-synthesized copy constructor is always shallow. Whenever deep copy is required, the class author has to write it (as you did).
Also, the signature of the copy constructor should be corrected to pass
the original object (here, b
) by const reference to prevent it from accidental modification:
Directory (Directory const & b)
^^^^^^
Upvotes: 1
Reputation: 18803
As the others have said, it is a deep copy.
In general when you use a copy constructor, the idea is to get a deep copy. Meaning that the copy does not share data with the original.
So Let's look at the following:
Directory B = new Directory(A);
A->DoSomething();
B->DoSomething();
When I read the above code, I expect that A->DoSomething()
only affects the data in A
. Likewise with B->DoSomething()
.
However, there can be exceptions to the deep copy rule. Maybe the data is large, so it might be inefficient to make a copy:
class LargeData {
int* PointerToLargeArray;
}
In the case above, you could implement the copy constructor by just copying pointers, rather than the data:
LargeData(LargeData& b)
{
PointerToLargeArray = b->PointerToLargeArray;
}
Upvotes: 0
Reputation: 5966
Telephone_Number
is declared as an int
in the class and copied by value (no references or pointers or anything), so this constructor is doing a deep copy.
Lots of information is available about copy constructors on Wikipedia or in any good C++ book, you should read something like that first and then see if you have any other specific questions.
It is probably worth the reading time, there are important rules that govern how copy constructors are used when initializing and assigning objects that you should understand.
Upvotes: 2
Reputation: 272297
That's a deep copy. Your int is copied by value and you're not copying a reference or pointer.
Upvotes: 0