I\'m trying to create a library for some work and am using operator overloading for assignment operation. Supposed X and Y are two instances of the class that has = overloaded t
If you declare a variable and initialize it on the same line, it will still call the copy constructor.
The are two initialization syntax:
X a;
X b(a);
X a;
X b = a;
There are slight differences as to what they mean, but in most cases they do the same. The difference is whether or not the compiler is guaranteed to avoid certain constructions/destructions. In either case, the copy constructor will be called, because your are constructing an object. I can't quite remember what the details are as for the difference in guarantees.
What's going on is that,
A p = q + r;
Calls the copy constructor of A, not the assignment operator. Yes, it's weird. It's the same as if you had typed this:
A p(q + r);
cases where a copy constructor is called:
when you return an object
when you pass an object to some function
X b(a);
X b = a;
cases where an assignment operator overload function is called is
X b; b=a;//where a is already existing object and already intialised.