Reputation: 18221
Trying to understand how to make assignment class operator. Member variable constant_name
is used to identify object. Member variable changeable_name
is used to get affect during assignment. I need this to understand who effects who and what is result of that. Each object gets unique constant_name
during creation.
class turbo
{
static string m;
public:
string changable_name ="";
string constant_name ="";
void nm()
{
m=m+"A";
changable_name=m;
constant_name=changable_name;
};
void printID()
{
printf("constant_name=%s changable_name=%s ",constant_name.c_str(),changable_name.c_str());
};
turbo() {
nm();
printID();
printf("default constructor \n");
};
turbo & operator = (turbo & value){ printID(); printf("= operator\n"); if (!(&value==this)) { changable_name = value.constant_name; } ; return *this; }
turbo (turbo&) {nm(); printID(); printf("copy constructor\n");}
};
string turbo::m;
int main( int argc, char ** argv )
{
turbo f;
turbo ff;
f=ff;
printf("--- result ---\n");
f.printID();
}
When =
operator is described in code at the top I have output:
constant_name=A changable_name=A default constructor
constant_name=AA changable_name=AA default constructor
constant_name=A changable_name=A = operator
--- result ---
constant_name=A changable_name=AA
Field changable_name
was coppyed from AA
to A
and that is fine.
Now if I remove reference sign in turbo value
:
turbo & operator = (turbo value){ printID(); printf("= operator\n"); if (!(&value==this)) { changable_name = value.constant_name; } ; return *this; }
I have following in output:
constant_name=A changable_name=A default constructor
constant_name=AA changable_name=AA default constructor
constant_name=AAA changable_name=AAA copy constructor
constant_name=A changable_name=A = operator
--- result ---
constant_name=A changable_name=AAA
New object AAA
was created and assigned to object A
. AA
had no influence at all. Why compiler decides to activate copy constructor and then assignment operator? I suppose Copy constructor was activated in right hand side of "=" operator.
Another case.
Now if I remove reference sign in turbo operator
:
turbo operator = (turbo & value){ printID(); printf("= operator\n"); if (!(&value==this)) { changable_name = value.constant_name; } ; return *this; }
I have following in output:
constant_name=A changable_name=A default constructor
constant_name=AA changable_name=AA default constructor
constant_name=A changable_name=A = operator
constant_name=AAA changable_name=AAA copy constructor
--- result ---
constant_name=A changable_name=AA
That completely confused me. I suppose AAA
was created in left hand side of =
. AAA has done no action at all in assignment. In result A
was assigned to AA
. But why it was needed to create AAA?
Upvotes: 0
Views: 83
Reputation: 30146
The test on &value == this
is pointless when the value
argument is passed by-value and not by-reference, because a new turbo
instance is created on the stack before the member function is called, and the address of that instance is obviously not equal to the address of the calling object.
Upvotes: 0
Reputation: 1197
If you don't define below functions in your class then compiler will provide its own versions
default constructor
copy constructor
assignment operator overload
destructor
Now in your second case you have removed the reference from the argument so its a case of pass by value and it was achieved by invoking copy-constructor.
And in third case it is return by value instead of by reference, so the copy ctor again gets called after the job of = operator is done and hence the output.
Upvotes: 0
Reputation: 409442
When you don't pass the argument by reference in the copy-assignment operator, then the compiler copies the argument you pass, and the function uses that copy. That the function called is the copy-assignment operator doesn't matter, it's the same for all functions.
Search and read more about pass-by-value and pass-by-reference.
As for the return value, the same thing happens there: The compiler creates a copy of the returned value. If you had properly implemented your copy-constructor to actually copy something, the result would have been different.
Upvotes: 1