Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a copy constructor in C++?

On page 6 of Scott Meyers's Effective C++, the term 'copy constructor' is defined. I've been using Schiltdt's book as my reference and I can find no mention of copy constructors. I get the idea but is this a standard part of c++? Will such constructors get called when a pass a class by value?

like image 430
Mike D Avatar asked Jan 30 '10 14:01

Mike D


People also ask

What is copy constructor example?

Copy Constructor is called in the following scenarios: When we initialize the object with another existing object of the same class type. For example, Student s1 = s2, where Student is the class. When the object of the same class type is passed by value as an argument.

What do copy constructors do?

Copy constructors are the member functions of a class that initialize the data members of the class using another object of the same class. It copies the values of the data variables of one object of a class to the data members of another object of the same class.

What is copy constructor Mcq?

Explanation: The copy constructor has the most basic function to initialize the members of an object with same values as that of some previously created object. The object must be of same class.

Why do we use & in copy constructor?

It's the copy constructor that's called when you pass an object by value. So if your copy constructor requires the instance to be copied before it can run, how can it ever run? The ampersand tells it to pass it by reference, so no copy is made.


1 Answers

A copy constructor is a constructor which does deep copy. You should write your own copy constructor when there is a pointer type variable inside the class. Compiler will insert copy constructor automatically when there is no explicit copy constructor written inside the code. The type of a copy constructor parameter should always be reference type, this to avoid infinite recursion due to the pass by value type.

below program explains the use of copy constructor

#include <iostream>
#pragma warning(disable : 4996)
using namespace std;
class SampleTest {
private:
    char* name;
    int age;
public:   
    SampleTest(char *name1, int age) {
        int l = strlen(name1);
        name = new char[l + 1];
        strcpy(this->name, name1);
        this->age = age;
    }
        SampleTest(const SampleTest& s) { //copy constructor 
            int l = strlen(s.name);
            name = new char[l + 1];
            strcpy(this->name, s.name);
            this->age = s.age;
        }
    void displayDetails() {
        cout << "Name is " << this->name << endl;
        cout << "Age is " << this->age << endl;
    }
    void changeName(char* newName) {
        int l = strlen(newName);
        name = new char[l + 1];
        strcpy(this->name, newName);
    }
};
int main() {
    SampleTest s("Test", 10);
    s.displayDetails();
    SampleTest s1(s);
    cout << "From copy constructor" << endl;
    s1.displayDetails();
    s1.changeName("Test1");
    cout << "after changing name s1:";
    s1.displayDetails();
    s.displayDetails();
    cin.get();
    return 0;
}
like image 163
Sarath Govind Avatar answered Nov 14 '22 18:11

Sarath Govind