C++ Tutorial
Class and Object
Reference
Inheritance and Derivation
Polymorphism and Virtual Functions
Operator Overloading
Template
Exception
Object Oriented Advanced
Input/Output Stream
File Operations
In this tutorial, we will discuss copy constructors in C++ and how they are used to create a new object as a copy of an existing object.
Introduction to Copy Constructors: A copy constructor is a special constructor in a class that is used to create a new object as a copy of an existing object. The copy constructor takes a reference to an object of the same class as its argument.
When to Define a Copy Constructor: The compiler automatically generates a default copy constructor for a class if you don't provide one. The default copy constructor performs a shallow copy of the object, which means it copies the member-wise values of the object.
In some cases, a shallow copy may not be suitable, and you need to define your own copy constructor. Common situations include:
Creating a Copy Constructor: To create a copy constructor, you need to define a constructor that takes a const reference to an object of the same class as its argument. In the constructor's body, you should specify how the object should be copied.
Example: Let's create an example that demonstrates how to define a copy constructor for a class with dynamically allocated memory.
#include <iostream> #include <cstring> class String { private: char* str; public: // Constructor with a const char* argument String(const char* s) { str = new char[strlen(s) + 1]; strcpy(str, s); } // Copy constructor String(const String& source) { str = new char[strlen(source.str) + 1]; strcpy(str, source.str); } // Destructor ~String() { delete[] str; } // Function to print the string void print() const { std::cout << str << std::endl; } }; int main() { // Create a String object using the constructor with a const char* argument String str1("Hello, world!"); str1.print(); // Output: Hello, world! // Create a String object using the copy constructor String str2 = str1; str2.print(); // Output: Hello, world! return 0; }
In the example above, we have defined a String
class that manages a dynamically allocated character array. We have provided a constructor that takes a const char*
argument, a copy constructor that takes a const reference to a String
object, and a destructor.
The copy constructor allocates new memory for the str
member and copies the characters from the source object's str
member. We then demonstrated the usage of the copy constructor to create a new String
object as a copy of an existing one.
How to implement a copy constructor in C++:
class MyClass { public: int data; // Copy constructor MyClass(const MyClass& other) : data(other.data) { // Constructor body } };
Copy constructor for dynamic memory allocation in C++:
class DynamicMemoryObject { public: int* dynamicData; // Copy constructor handling dynamic memory DynamicMemoryObject(const DynamicMemoryObject& other) : dynamicData(new int(*(other.dynamicData))) { // Constructor body } ~DynamicMemoryObject() { delete dynamicData; } };
Copy constructor and const correctness in C++:
class MyClass { public: int data; // Non-const copy constructor MyClass(MyClass& other) : data(other.data) { // Constructor body } // Const copy constructor MyClass(const MyClass& other) : data(other.data) { // Constructor body } };