C++ का Copy Constructor
In this page:
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Object Copying रोकना
आप '= delete' से copy constructor को explicitly delete करके एक class के objects को कभी copy होने से पूरी तरह रोक सकते हैं।
यह std::unique_ptr जैसे types द्वारा इस्तेमाल होने वाली standard technique है, जो exactly एक owner रखने के लिए designed हैं और कभी duplicate नहीं होने चाहिए।
उदाहरण: Preventing Object Copying
#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;
}
Login to try C/C++/Java/PHP code in the editor
- raw pointers के साथ default copy constructor पर depend करना, shallow copies और double delete देते हुए।
Obj(const Obj& other)की बजायObj(Obj other)लिखना।- copy constructor के साथ एक copy assignment operator define करना भूल जाना।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: