Hi...
wat is Copy Constructor ??? when it is invoked ???
Regards,
Sundar.G
Printable View
Hi...
wat is Copy Constructor ??? when it is invoked ???
Regards,
Sundar.G
Basically it is to create a deep copy of an object.
A copy constructor takes the class name.
Its called when you create an object from another. Or when you assign an object to another.
Most compilers will provide one if you do not create it yourself, a very basic one though.Code:// Assume these are implemented elsewhere, for simplicity sake
class Foo
{
public:
Foo(); // Regular
Foo(Foo &f); // Copy
...
};
Foo f1, f2;
Foo f3(f1); // Copy Constructor
f1.DoSomething();
f2 = f1; // Copy Constructor
Mind reading books? What are good books about C++ ?
Actually the assignment operator is called when making assignments to existing objects. It is a common cause of confusion. :)
To confuse even more (and hopefully clarify by doing so):Quote:
Originally Posted by TheCPUWizard
JeffCode:A a0;
A a1(a0); // copy constructor
A a2 = a1; // copy constructor
a2 = a1; // assignment operator
Hence the reason for me using the word "existing" in my post :D :D
And also the reason I used the word "clarify" in my post :DQuote:
Originally Posted by TheCPUWizard
Jeff