Deep Copy with Copy Constructor - c++

I have a task where I need to do the following.
I have 3 classes with A links to many B association A->*B->*C
A: Contains only -iA_ : int
B: Contains only -iB_ : int
C: Contains only -IC_ : int
The task is:
Implement the necessary codefor all classes so that A contains a copy structure for deep copies. Container type list. Setter and getter are not necessary!
Further constructors and destructors are not necessary!
If you use default implementations you have to justify this.
So I have implemented it is this correct?
class C
{
private:
int iC_;
}
class B
{
private:
int iB_;
std::list<C*> CList_;
public:
B(std::list<C*> CList) : CList_(CList)
{
}
}
class A
{
private:
int iA_;
std::list<B*> BList_;
public:
A(std::list<B*> BList) : BList_(BList)
{
}
A(const A& crA)
{
iA_ = crA.iA_;
}
}

Unfortunately, your code is not correct yet. In order to create deep copies of a class that contains pointers, you need to copy the objects that those pointers point to as well. Here is a simple example with just A and B:
class B
{
private:
int iB_;
public:
// you will not be able to default this in your code
B(const B& other) = default;
};
class A
{
private:
int iA_;
vector<B*> BList_;
public:
A(const A& other) : iA_{other.iA_}, BList_{}
{
// fill up the BList_ with copies of other's BList_ items
for (auto b : other.BList_) {
// call B's copy constructor
B* copyPtr = new B{*b};
BList_.push_back(copyPtr);
}
}
A& operator=(const A& other)
{
iA_ = other.iA_;
// destroy all the current items in BList_
while (!BList_.empty()) { delete BList_.pop_back(); }
// fill up the BList_ with copies of other's BList_ items
for (auto b : other.BList_) {
// call B's copy constructor
B* copyPtr = new B{*b};
BList_.push_back(copyPtr);
}
}
~A()
{
// destroy all the current items in BList_
while (!BList_.empty()) { delete BList_.pop_back(); }
}
};
With your code, you will need to implement a similar thing with B's copy constructor as well to make sure it makes copies of everything in CList_, only then will you have a true deepcopy.
Edit: As #Alan Birtles noted, in such an implementation you should also define the copy assignment operator and destructor to prevent memory leaks.

Related

Delegating to a default C++ constructor

Assume a C++ class A with members that can all be copied by the their respective copy constructors. We can rely on the default copy constructor for A to copy its members:
class A {
private:
A(const A&) = default; // We don't really need this line
int a;
B b;
double c;
}
But now let's assume that I want to "extend" (or annotate) the default constructor of A so that in addition to copying its members, it does something else, e.g. writes a line to a log file.
I.e. I'd like to write something like:
class A {
public:
A(const A& a) : A::default(A) {
print("Constructing A\n");
}
private:
// like before
}
Unfortunately that is not correct C++ syntax.
So is there a syntax which allows delegating to a default C++ constructor while explicitly defining the same constructor?
The simplest is to move all members into a base class and write the message in a derived class:
class A_helper {
private:
int a;
B b;
double c;
};
class A : public A_helper {
public:
A() = default;
A(const A& a) : A_helper(a) {
print("Constructing A\n");
}
A(A&& a) : A_helper(std::move(a)) {
print("Constructing A\n");
}
};
You can delegate to the default constructor like you would default-initialize a member variable:
A(const A&) : A()
{
...
}
As often, "We can solve any problem by introducing an extra level of indirection." ( "…except for the problem of too many levels of indirection,"):
struct VerboseA
{
VerboseA() = default;
VerboseA(const VerboseA&) { print("Constructing A\n"); }
VerboseA(VerboseA&&) { print("Constructing A\n"); }
};
And then
class A : VerboseA // EBO (Empty Base Optimisation)
{
private:
A(const A&) = default; // We don't really need this line
int a;
B b;
double c;
};
or in C++20
class A {
private:
A(const A&) = default; // We don't really need this line
[[no_unique_address]]VerboseA verbose; // "EBO" equivalent with attribute
int a;
B b;
double c;
};

How to write copy constructor for Class with object of another class

I am having trouble writing this fairly simple program. I have two class A and B. B has an object of A. I need to write Copy constructor of B so that two instances of B will have the different instance of A. Is there any neat way to do this? One thing to do is getting all the member variable of parm, creating a new A object and assigning those member variables. But if the class is having more member variables then it is a problem. How to write this in a simple way?
class A
{
public:
int data;
A()
{
}
A(int parm) : data(parm)
{
}
A(const A&parm)
{
this->data = parm.data;
}
A& operator = (const A& parm)
{
if (this != &parm)
{
this->data = parm.data;
}
return *this;
}
~A()
{
cout << "A is destroyed";
}
};
class B
{
public:
A *a;
B()
{
a = new A(10);
}
B(const B&parm)
{
// How to copy the value of parm so this and parm have different A object
// this.a = parm.a --> both this and parm points to same A object
}
B& operator = (const B&parm)
{
if (this != &parm)
{
this->a = parm.a;
}
return *this;
}
~B()
{
// Null check
delete a;
}
};
B's copy constructor needs to allocate a new A object that copies data from the source parm.a member, which you can do using A's copy constructor:
B(const B &parm) {
a = new A(*(parm.a));
}
Also, in B's assignment operator, both A objects are already allocated, so just invoke A's assignment operator:
B& operator=(const B &parm) {
if (this != &parm) {
*a = *(parm.a);
}
return *this;
}
That being said, you can greatly simplify the code if you get rid of the pointer and let the compiler handle the memory allocations and copies for you:
class A {
public:
int data;
A(int parm = 0) : data(parm) { }
~A() { cout << "A is destroyed"; }
};
class B {
public:
A a;
B() : a(10) { }
};

Making copy of any pointer member varible in copy constructor

I have a class B which has a member that is a pointer to an object of a A class. When using copy constructor on an object of type A, it is copied but the member variable is not.
Is there any way to copy an A object and to automatically make a copy of its B member?
The following code shows the problem I'm triying to explain:
class A
{
public:
A(char t_name)
{
name = t_name;
}
~A()
{
}
char name;
};
class B
{
public:
A* attribute;
B()
{
attribute = new A('1');
}
~B()
{}
};
int main()
{
B* b_variable = new B;
B* b_copy = new B(*b_variable);
return 0;
}
When using copy constructor on an object of type A, it is copied but the member variable is not.
Your code never calls any copy constructor in class A.
Your code calls a copy constructor in class B and it does exactly what is is supposed to, i.e. copies the value of attribute which is a pointer to a class A object.
In other words - after executing your code, you have two instances of class B and one class A instance. In the two class B instances attribute points to the same class A instance.
This is (most likely) not what you want.
As many already has pointed out (e.g. see #lostbard answer), you'll need a copy constructor in class B to do a deep-copy. A deep-copy is needed because class B have a pointer member.
Also you should do some clean up in class B destructor and in main.
#include <iostream>
using namespace std;
class A
{
public:
A(char t_name)
{
name = t_name;
}
~A()
{
}
char name;
};
class B
{
public:
A* attribute;
B()
{
attribute = new A('1');
}
/** Copy constructor */
B(const B &copy)
{
// Make a new instance of class A
attribute = new A(*copy.attribute);
}
/** Assignment operator */
B& operator= (const B& other)
{
// Delete the existing class A instance
delete attribute;
// and create a new as a copy of other.attribute
attribute = new A(*other.attribute);
}
~B()
{
// Delete the class A instance
delete attribute;
}
};
int main()
{
B* b_variable = new B;
B* b_copy = new B(*b_variable);
// Delete the two class B instances
delete b_variable;
delete b_copy;
return 0;
}
There is no need for a copy constructor in class A. The default generated will do as Class A has no pointer members.
EDIT
As pointed out by #Slava you should always implement a assignment operator when you make a copy constructor (rule of three) so I added it to the code above.
Some like the rule of three to be the rule of five so it also include move. Read more here: https://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)
While there are many solutions, I'd bet sooner-or-later you'll end up with implementing the usual virtual a.clone();. That works flawless when you'll have derived classes of A (which is more-or-less the only legitimate reason why you're keeping A as a pointer to a heap-allocated object and not as a value member :) ).
Note that, when you're implementing clone() in your hierarchy, that C++ supports covariant pointer return types. Thus, if base has a virtual that returns e.g. Clonable*, then A's same method can return A* and A's descendant ADerived can return ADerived*. Feel free to understand 'can' as 'should' for the case of clone().
Create a copy constructor for A and for B:
class A
{
public:
A(char t_name)
{
name = t_name;
}
A(const A& copy)
{
name = copy.name;
}
~A()
{
}
char name;
};
class B
{
public:
A* attribute;
B()
{
attribute = new A('1');
}
B(const B &copy)
{
attribute = new A(*copy.attribute);
}
~B()
{}
};

C++ copy constructor clear up on vector<Base*> of Derived*

I have a class which uses a class of base pointers to derived objects, so I need to have my own desructor for deleting the vector's elements and custom copy and assignment funcitons. I'm not entirely sure about the preferred way of implementing a structure like below and writing the right copy and assignment constructors and destructors for it. May I ask you to guide me? I've read a lot and searched but I'm still not sure.
class Base
{
public:
Base();
~virtual Base();
int a;
int type; // Derived1 or Derived2
std::string b;
...
}
class Derived1 : public Base
{
public:
Derived1();
Derived1(const Base* a);
virtual ~Derived1();
}
class Derived1
{
Derived1::Derived1(const Base *a) : Base(a)
{
}
}
class Derived2 : public Base
{
public:
Derived2();
Derived2(const Base* a);
virtual ~Derived2();
std::string d1;
std::string d2;
int d3;
}
class Derived2
{
Derived2::Derived2(const Base *a) : Base(a) {
this->d1 = ((Derived2*)a)->d1;
this->d2 = ((Derived2*)a)->d2;
this->d3 = ((Derived2*)a)->d3;
}
}
class A
{
public:
A();
~A();
A(const A& a);
A& operator = (const A& a);
std::string someString;
std::vector<Base*> vect;
}
A::~A() {
std::vector<Base*>::iterator it = vect.begin();
while (it != vect.end()) {
delete (*it);
it++;
}
A::A(const A &a)
{
someString = a.someString;
for(std::size_t i = 0; i < a.vect.size(); ++i {
someString = a.someString;
Base* base = a.vect.at(i);
if(base->type == base::TypeD1) {
base = new Derived1( a.vect.at(i) );
vect.push_back( base );
}
else {
base = new Derived2( a.vect.at(i) );
vect.push_back( base );
}
}
}
Your loop in the destructor is fine in practice, and is the
usual solution. Formally, it is undefined behavior, since you
are leaving objects in the vector (pointers to deleted objects)
which aren't copiable, but in practice: the vector won't copy
them unless you resize it to something bigger, or insert or
erase on it. If you really want to avoid the undefined
behavior:
for ( auto current = vect.begin(); current != vect.end(); ++ current ) {
Base* tmp = *it;
*it = nullptr;
delete tmp;
}
But this is one case where I probably wouldn't bother (and
I tend to be more sensitive to undefined behavior than most).
First, do you actually need to copy and assign objects of type A? If no, the simple solution is:
class A
{
public:
A();
~A();
A(const A&) = delete;
A(A&&) = default;
A& operator=(const A&) = delete;
A& operator=(A&&) = default;
// ...
};
If yes, then you want some polymorphic way of copying the elements of the vector. (Any time at all you have if (b->type == Base::TypeD1) { do_this(); } else { do_that(); }, stop and think if it would make sense to add a virtual function for do_this/do_that. The else-if-heimer's way doesn't allow for future new derived classes; the virtual way does.)
class Base
{
public:
// ...
virtual Base* clone() const = 0;
// ...
};
class Derived1 : public Base
{
public:
virtual Derived1* clone() const;
};
Derived1* Derived1::clone() const {
return new Derived1(*this);
}
The copy assignment operator of A will need to destroy the old contents of the lhs, just like the destructor, and then copy the new contents over, just like the copy constructor. So let's put those two operations in private functions:
class A
{
public:
A();
~A();
A(const A&);
A(A&&) = default;
A& operator=(const A&);
A& operator=(A&&) = default;
// ...
private:
void destroy_contents();
void copy_from(const std::vector<Base*>& v);
};
void A::destroy_contents() {
std::vector<Base*>::iterator it = vect.begin();
while (it != vect.end()) {
delete (*it);
++it;
}
vect.clear();
}
void A::copy_from(const std::vector<Base*>& v) {
std::vector<Base*>::const_iterator it = v.begin();
while (it != v.end()) {
vect.push_back((*v)->clone());
++it;
}
}
A::~A() { destroy_contents(); }
A::A(const A& a) :
someString(a.someString),
vect()
{
copy_from(a.vect);
}
A& A::operator=(const A& a) {
if (this != &a) {
someString = a.someString;
destroy_contents();
copy_from(a.vect);
}
return *this;
}
If you have a vector of any pointer type, the vector does not know anything about the type behind the pointer, nor does it care: The vector operates on the pointers only, copying them to a new location if need be, but never even touching the objects themselves. As such, it is your responsibility to destroy the objects themselves.
As James Kanze points out, there is a slight danger of undefined behavior when handling invalidated pointers. However, since the vector is not used in any way after deleting the objects it holds, no undefined behavior is invoked in the code you've given (the vector won't need to reallocate its memory so it won't need to assign invalidated pointers, and destruction of pointers is a noop). As such, your destructor of class A is perfectly fine.
The copy constructor of class A, however, is unnecessarily complicated and an ample source of errors (it needs to be updated whenever a new derived class is defined!). The best way to do this is to use a clone() function:
class Base {
public:
//...
virtual Base* clone() const = 0; //Returns a new copy of the object. Pure virtual if the Base class is abstract.
};
class Derived1 : public Base {
public:
Derived1(const Derived& a);
virtual Derived* clone() const {
return new Derived1(*this);
}
};
If you make clone() pure virtual in the base class, you have the guarantee that your compiler will complain if you forget to implement it in any derived class. With that, the copy constructor of class A is next to trivial:
A::A(const A &a) {
someString = a.someString;
for(std::size_t i = 0; i < a.vect.size(); ++i {
vect.push_back(a.vect[i]->clone());
}
}

Prevent instantiation of an object outside its factory method

Suppose I have a class with a factory method
class A {
public:
static A* newA()
{
// Some code, logging, ...
return new A();
}
}
Is it possible to prevent the instantiation of an object of this class with a new, so that factory method is the only method to create an instance of the object?
Sure; just make the constructor private (protected if this is a base class):
class A {
public:
static A* newA()
{
// Some code, logging, ...
return new A();
}
private:
A() {} // Default constructor
};
You should make the copy constructor private/protected as well, if required.
And as always, you should strongly consider returning a smart pointer rather than a raw pointer, in order to simplify memory management issues.
You may also want to make the copy constructor private as well or with new C++11 syntax you can explicitly tell the compiler to not copy it and make the default constructor private with something like this:
struct NonCopyable {
NonCopyable & operator=(const NonCopyable&) = delete;
NonCopyable(const NonCopyable&) = delete;
NonCopyable() = default;
};
class A : NonCopyable {
public:
static std::shared_ptr<A> newA()
{
// Some code, logging, ...
return std::make_shared<A>();
}
private:
A() {} // Default constructor
};
The C++03 way was usually something like this:
class A {
public:
static A* newA()
{
// Some code, logging, ...
return new A();
}
private:
A() {} // no outsider default constructor
A(const A& rhs); // no copy
A& operator=(const A& rhs); // no assignment
};
int main()
{
A x; // C2248
A y(x); // C2248
x = y; // C2248
A* p = A::newA(); // OK
std::cin.get();
return 0;
}