← Back to C++ Course | Chapter 8: OOP Core | Lesson 12 of 14

C++ का Copy Constructor

एक copy constructor एक existing object की एक copy की तरह एक नया object बनाता है, एक page photocopy करने जैसा। Care चाहिए ताकि दोनों copies same hidden data share न करें।
Syntax
cpp
class ClassName {
public:
  ClassName(const ClassName &other) {
    // copy each member from other
  }
};

ClassName copy = original;

Default Copy Constructor

अगर आप खुद एक copy constructor नहीं लिखते, compiler automatically एक default generate करता है जो source object से नए में एक shallow, member-by-member copy perform करता है।

यह simple data के लिए ठीक काम करता है लेकिन एक class के एक raw pointer own करते ही dangerous हो जाता है।

उदाहरण: Default Copy Constructor

cpp
#include <iostream>

class Point {
public:
	int x = 5;
};

int main() {
	Point p1;
	Point p2 = p1; // compiler-generated shallow member-by-member copy
	std::cout << p2.x << std::endl;
	return 0;
}

Custom Copy Constructor Define करना

आप अपना खुद का copy constructor define कर सकते हैं यह precisely control करने के लिए कि एक नया object एक existing से कैसे initialize होता है; यह अपने parameter की तरह same class के एक const object का reference लेता है, जो इसे बिना source की अपनी copy का risk लिए (या इसकी ज़रूरत के) पढ़ने देता है।

उदाहरण: Defining Custom Copy Constructor

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Point
class Point {
// Members below can be accessed from outside the class
public:
	int x;
	Point(int val) : x(val) {}
	Point(const Point &other) : x(other.x) {
		// Print to the console with cout
		std::cout << "Copy constructor called" << std::endl;
	}
};

// Program execution starts in main()
int main() {
	Point p1(5);
	Point p2 = p1;
	// Print to the console with cout
	std::cout << p2.x << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Shallow Copy बनाम Deep Copy

एक shallow copy एक pointer के address को duplicate करता है, original और copy दोनों को exact same memory की ओर point करते हुए छोड़ते हुए — dangerous, क्योंकि एक को delete करना दूसरे को एक dangling pointer के साथ छोड़ देगा।

एक deep copy इसकी बजाय fresh memory allocate करता है और actual data duplicate करता है, ताकि हर object अपनी independent copy own करे।

उदाहरण: Shallow Copy vs Deep Copy

cpp
#include <iostream>

class Buffer {
public:
	int *data;
	Buffer(int val) { data = new int(val); }
	Buffer(const Buffer &other) {
		data = new int(*other.data); // deep copy: separate memory
	}
	~Buffer() { delete data; }
};

int main() {
	Buffer b1(10);
	Buffer b2 = b1;
	std::cout << *b1.data << " " << *b2.data << std::endl;
	return 0;
}

Function Calls में Copy Constructor

Copy constructor automatically fire होता है जब भी एक object value से एक function में pass किया जाता है, या इससे बाहर value से return किया जाता है, यही वजह है कि बड़े objects को value से pass करना surprisingly expensive हो सकता है — move semantics kick in न करने पर हर call एक पूरी copy trigger करती है।

उदाहरण: Copy Constructor in Function Calls

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Point
class Point {
// Members below can be accessed from outside the class
public:
	int x;
	Point(int val) : x(val) {}
	Point(const Point &other) : x(other.x) {
		// Print to the console with cout
		std::cout << "Copied" << std::endl;
	}
};

void showPoint(Point p) {
	// Print to the console with cout
	std::cout << p.x << std::endl;
}

// Program execution starts in main()
int main() {
	Point p(5);
	showPoint(p);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Object Copying रोकना

आप '= delete' से copy constructor को explicitly delete करके एक class के objects को कभी copy होने से पूरी तरह रोक सकते हैं।

यह std::unique_ptr जैसे types द्वारा इस्तेमाल होने वाली standard technique है, जो exactly एक owner रखने के लिए designed हैं और कभी duplicate नहीं होने चाहिए।

उदाहरण: Preventing Object Copying

cpp
#include <iostream>

class Resource {
public:
	Resource() {}
	Resource(const Resource &) = delete;
};

int main() {
	Resource r1;
	// Resource r2 = r1; would fail to compile: copy constructor deleted
	std::cout << "Resource created, copying disabled" << std::endl;
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. raw pointers के साथ default copy constructor पर depend करना, shallow copies और double delete देते हुए।
  2. Obj(const Obj& other) की बजाय Obj(Obj other) लिखना।
  3. copy constructor के साथ एक copy assignment operator define करना भूल जाना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.