Reputation: 1135
I have the following query on copy constructor and assignment constructor for a class with both structures & pointers member variables.
Here is my class
class myClass{
public:
Calculator mCalc; // Calculator is a class that I defined elsewhere
struct Sstatus{
bool add_flag;
int error_code;
CvMat* matrix; // I am using OpenCV here for matrix handle
double params[6];
};
// class function
myClass(void);
~myClass(void);
protected:
int index;
BasedClass* interface;
CvMat* matrix_int;
private:
int calc_index;
bool* done;
};
And my class function is as follows
myClass::myClass(void):mCalc(0),
index(0),
matrix_int(0),
calc_index(0),
done(0)
{
interface = new DerivedClass(); // derived class is extended by the base class
}
myClass::~myClass(void){
delete interface;
}
// defining copy constructor
myClass::myClass(const myClass& o):mCalc(o.mCalc),
index(o.index),
calc_index(o.calc_index),
done(o.done)
{
// assigning new memory for member pointers for copying
matrix_int = new CvMat();
*matrix_int = o.matrix_int;
interface = new DerivedClass();
*interface = o.interface;
done = new bool();
*bool = o.done;
}
// defining assignment operator
myClass::myClass& operator=(const myClass& o)
{
if(this != &o)
{
mCalc = o.mCalc;
index = o.index;
calc_index = o.calc_index;
// assigning new memory for member pointers for copying
matrix_int = new CvMat();
*matrix_int = o.matrix_int;
interface = new DerivedClass();
*interface = o.interface;
done = new bool();
*bool = o.done;
}
return *this;
}
A few questions I have
Thanks
Additional questions: If I have used new in my copy constructor, would I be require to delete it in the destructor?
Upvotes: 2
Views: 2582
Reputation: 141554
What you should do is design your class so that it does not require any user-defined copy-constructor, assignment-operator or destructor. Then there is no chance that you can make mistakes in your code for those functions. Further, the implicitly-generated move constructor will work.
Let's fix your class to work this way. There are four pointers we have to deal with:
bool* done;
This is easy enough, just change to bool done;
and its initializer should be false
. Set to true
when needed.
CvMat* matrix_int;
CvMat* matrix;
In your existing code you copy a matrix just by using CvMat
's assignment operator, and you default-construct them. If that is actually correct then you can fix this just by changing to CvMat matrix;
and CvMat matrix_int;
.
BasedClass* interface;
Here there is a bit of a problem - since you have customized copy behaviour here (creating a new DerivedClass then doing operator=) there's no standard wrapper for that.
You could make a little helper class for managing this pointer. You have to write copy-constructors etc. for this, but then that code is self-contained, you don't have to write a bunch of useless boilerplate code for the remainder of your class.
Note - using operator=
to copy the derived class is suspicious. That will only work if all of the objects are derived from DerivedClass
, in which case you may as well use DerivedClass
as the type.
Upvotes: 1