Copy Constructor
Title
Question
What is the main role of Copy Constructor in the concept of constructor and is it save the memory for system?
Advanced-Cpp Constructor-And-Destructor 00-01 min 50-60 sec
Answers:
Dear Mann_19,
A copy constructor is used for creating a new object from an existing one, without explicitly copying each member variables. It is not meant for saving memory, but it is just a matter of neat copying provided by c++.
nags
A copy constructor is a member function which initializes an object using another object of the same class. A copy constructor is called whenever a new variable is created from an object.
A copy constructor has the following general function prototype:
ClassName (const ClassName &old_obj);
Copy constructor dynamically allocates memory for the copy first and then copies the actual value. In a deep copy, both objects which have to copy and another which has to be copied will be having different memory locations. So, the changes made to one will not affect another. This is used by a user-defined copy constructor.
A copy constructor is a member function that initializes an object using another object of the same class.This is the Role of copy constructor..
SACHIN VALAVI 20th February 2022
Login to add comment