How to force a shallow memberwise copy in an assignment operator?

Hi,

Perhaps you can help me finding a way to make a shallow memberwise copy of any element of a class in one go. My question is the following. Given a class like:

class TestClass
{
public:
double b;
double ar[3];
};

When I created and copy to objects like this:

TestClass t1;
t1.b= 3.14;
for(int i=0; i<3; ++i)
t1.ar[i]= i;

TestClass t2;
t2= t1;

At the end the default assignment operator works perfectly, both attributes
(b & ar) are identical.

Now I want to implement an assignement operator (not needed in this case since
I do not use dynamic memory allocation).

TestClass& operator=(const TestClass& obj)
{
if( this != &obj )
{
b= obj.b;
for(int i=0; i<3; ++i)
ar[i]= obj.ar[i];
}
return *this;
}

And it works as well. My question is:

Is there a way to make this copy using the default assignment operator inside
my own assignment operator? I know the C++ compiler knows how to do it since
by default working.

In my real C++ class I have many variables and static arrays (>50) in the class and only a couple of dynamic array allocations (I need the = operator) and then I am forced to copy member by member all
the static variables in the = operator.
At the end is crazy, if tomorrow I add a new variable to the class and I forget to include it in the assignement operator I will get into trouble.

Thanks in advance
Pedro
In my real C++ class I have many variables and static arrays (>50) in the class and only a couple of dynamic array allocations (I need the = operator) and then I am forced to copy member by member all
the static variables in the = operator.

If member variables of your class are declared static, then you should not have to copy them between instances of your class. But I have a feeling that you already know this and I'm just being an idiot, so would you mind rephrasing your question?
Excuse me, I did not meant static array but regular arrays (eg double v[5]). My question is whether may I use the intelligence of the default assignment operator that is working perfectly when I create my own assignment operator and take care only of the couple of dynamic arrays I use. I do not want to write something like:

TestClass& operator=(const TestClass& obj)
{
if( this != &obj )
{
b= obj.b;
for(int i=0; i<3; ++i)
ar[i]= obj.ar[i];
c= obj.c;
d= obj.d;
e= obj.e;
...
z= obj.z
}
return *this;
}

but something like:


TestClass& operator=(const TestClass& obj)
{
if( this != &obj )
{
callDefaultAssignmentHere();
}
return *this;
}
If you broke this up into two classes with the regular (non-dynamic) variables in a parent class you should be able to use the default assignment operator from that parent class to fill out those variables via a base class pointer to the derived one.
rule of zero. http://en.cppreference.com/w/cpp/language/rule_of_three
change your dynamic arrays to std::vector
now you don't need to write the copy/move constructor, assignment operator or destructor.
Last edited on
Topic archived. No new replies allowed.