Hi all,
In which cases does the compiler forces us to have a copy constructor and an assignment operator? I mean, when we have to define both explicitly.
Thanks in advance
This is a discussion on question about copy constructor and assignment operator within the C++ Programming forums, part of the General Programming Boards category; Hi all, In which cases does the compiler forces us to have a copy constructor and an assignment operator? I ...
Hi all,
In which cases does the compiler forces us to have a copy constructor and an assignment operator? I mean, when we have to define both explicitly.
Thanks in advance
Never? The whole point of writing either of those things is that you decide if you need them to do something different than what the compiler gives you, because there are different kinds of copies.
The compiler assumes that it can just assign all the members like this when you use assignment:
foo = rhs.foo;
bar = rhs.bar;
and so on. Memory management is the most common reason this will not be appropriate and then you will need to write your own assignment operator. For instance, an object may have raw pointers as members that should be handled. They teach that stuff in class.
Writing a copy constructor is usually a prerequisite for implementing assignment which looks like this:
where swap is a member function that swaps two objects. It may be the easiest way but it is not the only way, of course.Code:object & operator = (object & lhs , const object & rhs) { return object(lhs).swap(rhs); }
Originally Posted by phantomotap
Most of what you said I already know, but I was asked that question and couldn't really find a case and still can't. Maybe it was a trick question.
By the way, whiteflags' example is a little flawed in that, at least with respect to the current version of the C++ standard, an overloaded copy assignment operator must be a non-static member function. Furthermore, swap functions typically have a void return type. As such, you are more likely to see this:
or:Code:object& object::operator=(object rhs) { swap(rhs); return *this; }
A check for this != &rhs might be performed.Code:object& object::operator=(const object& rhs) { object temp(rhs); swap(temp); return *this; }
Last edited by laserlight; 04-20-2010 at 08:40 AM. Reason: Oops: cannot swap with a const.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
Well, thanks for the replies whiteflags and laserlight. Regarding the question maybe I didn't understand it correctly, because I can't find any answer/hints to it.
You might find these interesting:Originally Posted by sugarfree
Originally Posted by C++03 Section 12.8 Paragraph 7
Originally Posted by C++03 Section 12.8 Paragraph 12
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
This basicaly says when you should write them:
http://en.wikipedia.org/wiki/Rule_of...2B_programming)
rule of three@Everything2.com
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
You're both crazy. The "rhs" should not be constant.By the way, whiteflags' example [...] must be a non-static member function.
Soma
Last edited by phantomotap; 04-20-2010 at 08:36 AM. Reason: This, by the way, is a joke.
Heheh, oh yeah, otherwise we'll be trying to swap with something that is const. Fixed.Originally Posted by phantomotap
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way