Hi all:
In C++, what are the common usages of a copy constructor? I know construtor can be used to initilize the object, but have not heard anything about how to use a copy construtor.
Thanks
Hi all:
In C++, what are the common usages of a copy constructor? I know construtor can be used to initilize the object, but have not heard anything about how to use a copy construtor.
Thanks
Two common places for a programmer to use the copy constructor are:
MyClass one; //uses default constructor
MyClass two(one); //uses copy constructor
MyClass three = one; //uses copy constructor
The compiler will also use the copy costructor and you aren't aware of it, unless you are intimately familiar with how a compiler works; which is why all classes need a copy contructor and the compiler will provide one if you don't.
Don't forget
return three; // uses copy constructor
some_function(three);
// uses copy constructor unless passed by reference
Of course, a compiler could behave magically and optimise away a copy constructor call.
How can we write an object into the file. and how can we retrieve it later
Please don't resurrect a year old thread with an unrelated question.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.