will copy constructor does object initilization using another already created object? I understand that it can be applied for object initilization and not for assignment. Is it correct? please clarify me
will copy constructor does object initilization using another already created object? I understand that it can be applied for object initilization and not for assignment. Is it correct? please clarify me
If I understand correctly, the following is correct:
assignment also works fine:Code:Object obj1(whatever); Object obj2(obj1);
Code:obj2 = obj1
Devoted my life to programming...
Normally yes, but of course what the copy constructor actually does depends on its implementation.Originally Posted by leo2008
By definition, the copy constructor is invoked for copy construction, not assignment.Originally Posted by leo2008
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
The wrinkle is that the statement
also uses the copy constructor to initialise a, and does not invoke operator=().Code:// assume b is of type Object Object a = b;
However
uses the default constructor to construct a, followed by an assignment (operator=()).Code:// assume b is of type Object Object a; a = b;
Further wrinkles are that there are ways in which some or all of these behaviours may be prevented or (for want of a better word) adjusted. The examples above assume
1) All of the constructors and operators mentioned are defined (whether implicitly or explicitly) and accessible to the code above.
2) The constructors have not been declared using the explicit keyword.
Huh? What are you talking about now? What is your actual question?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
"Bitwise copy constructors" doesn't mean anything to me sorry.
There can only be one copy constructor, because the copy constructor has a specific function signature.
T(const T& rhs);
That's it.
Ordinarily, a copy constructor that a compiler would generate is just one that calls the other copy constructors for each data member in the class. The default copy constructor usually works: Plain old data types have copy constructors, as do many STL types. Where you run into trouble is when your class contains raw pointers.
In the default copy constructor, a raw pointer member simply copies the pointer, such that the new pointer member will have the same address value as the older pointer. This is called a "shallow copy". The worry is that one of the objects will fall out of scope and get destroyed, thus destroying the memory the pointer points to, and leaving the other pointer that shares that memory dangling.
So, to solve that issue, you can rewrite the copy constructor to do something else. Perhaps use new when you construct the new object and so on, which is called a "deep copy".
There are other reasons to rewrite the copy constructor, just so you know. You have to rewrite whenever the default behavior is not what you want. There are such semantics as copy-on-write and more. I don't really want to get into that area if it's not necessary. The bottom line is, if you write a copy constructor, that one will be used. If you don't write a copy constructor, then the default one will be generated, and that one will be used.
Consider also the rule of three.
Last edited by whiteflags; 03-17-2013 at 07:51 PM.
Your question is based on an incorrect assumption.
Whilst simple examples of where the copy-constructor is omitted may appear to perform bit-wise copy of the members, it in fact does not.
It does member-wise copy. E.g. if one of the members is a std::string, then it will call the std::string copy-constructor to copy that string, which will likely involve memory allocation etc. Only those members that are built-in types will effectively result in a bit-wise copy.
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"