Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 69

Copy Constructor in C++

In this lesson: When copy constructors run in C++, shallow vs deep copy and the rule of three — with output. In Hindi & English.

What is a copy constructor?

A copy constructor builds a new object as a copy of an existing one. It has a special signature — it takes a reference to another object of the same class:

C++
class Point {
public:
    int x, y;
    Point(int a, int b) : x(a), y(b) { }
    Point(const Point &p) : x(p.x), y(p.y) {   // copy constructor
        cout << "Copied\n";
    }
};

When is it called?

  • Initialising one object from another: Point b = a;
  • Passing an object to a function by value.
  • Returning an object by value from a function.

The compiler-provided copy

If you write none, the compiler makes one that copies each member. For simple classes (only plain values) this is perfect and you need not write your own.

Shallow copy problem

The trouble appears when a class holds a pointer. The default copy duplicates the pointer value, so both objects point at the same memory:

C++
class Buffer {
public:
    int *data;
    Buffer(int v) { data = new int(v); }
    ~Buffer() { delete data; }
    // no copy constructor → shallow copy
};
// Buffer b2 = b1;  → both share the same data pointer!
// when both destruct → double free (crash)

Writing a deep copy

A deep copy allocates fresh memory so each object owns its own data:

C++
class Buffer {
public:
    int *data;
    Buffer(int v) { data = new int(v); }
    Buffer(const Buffer &b) {          // deep copy
        data = new int(*b.data);       // new memory, copy value
    }
    ~Buffer() { delete data; }
};

Now each Buffer has its own data, so destroying one does not affect the other.

The rule of three

If a class needs a custom copy constructor, it almost always also needs a custom destructor and copy assignment operator — the rule of three. Modern C++ often sidesteps all of this by using smart pointers, which copy safely on their own.

Common mistakes

  • Relying on the default shallow copy for a class that owns a pointer.
  • Forgetting const and the reference in const ClassName&.
  • Writing a copy constructor but forgetting the matching destructor/assignment (rule of three).
  • Passing big objects by value (triggering copies) when a const reference would do.
🏋️ Practice

Write a class holding a dynamically allocated array. Give it a deep-copy copy constructor and a destructor. Create an object, copy it, change one copy, and confirm the other is unaffected.

Summary

  • A copy constructor makes a new object from an existing one.
  • It is called on copy-initialisation and pass/return by value.
  • The default copy is shallow — unsafe for classes owning pointers.
  • Write a deep copy so each object owns its own resource.
  • A custom copy constructor usually means the rule of three applies.

Frequently Asked Questions

What is a copy constructor in C++?
A copy constructor is a special constructor that creates a new object as a copy of an existing one. It takes a reference to another object of the same class, written ClassName(const ClassName &other), and is used whenever an object is initialised from another.
When is the copy constructor called in C++?
It is called when you initialise an object from another object, pass an object to a function by value, or return an object by value. In each case a new copy of the object is created using the copy constructor.
What is the difference between shallow copy and deep copy in C++?
A shallow copy duplicates member values directly, so if a member is a pointer, both objects end up sharing the same memory. A deep copy allocates new memory and copies the pointed-to data, giving each object its own independent copy. Classes that own pointers usually need a deep copy.
Does C++ provide a copy constructor automatically?
Yes. If you do not write one, the compiler generates a default copy constructor that copies each member. This works fine for simple classes, but it performs a shallow copy, which is unsafe for classes that manage raw pointers or other resources.
Why does a class with a pointer need a custom copy constructor?
Because the default shallow copy makes two objects share the same pointer, so when one is destroyed and frees the memory, the other is left with a dangling pointer, and destroying it too causes a double free. A deep-copying copy constructor gives each object its own memory to avoid this.
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topicOperator Overloading Your progress: 69 / 81 (85%) · Lesson 70 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free