通常,创建对象的副本意味着创建具有相同字面量值、数据类型和资源的对象的精确副本。
- 复制构造函数
- 默认赋值运算符
// Copy Constructor
Geeks Obj1(Obj);
or
Geeks Obj1 = Obj;
// Default assignment operator
Geeks Obj2;
Obj2 = Obj1;
根据对象持有的动态内存等资源,我们需要执行浅拷贝或深拷贝以创建对象的副本。通常,如果对象的变量已动态分配,则需要进行深度复制以创建对象的副本。
浅拷贝:
在浅拷贝中,通过简单地复制原始对象的所有变量的数据来创建对象。如果在内存的堆部分中没有定义对象的任何变量,这将很有效。如果某些变量是从堆部分动态分配的内存,则复制的对象变量也将引用相同的内存位置。
这将产生模糊性和悬空指针的运行时错误。由于两个对象将引用相同的内存位置,因此一个对象所做的更改也将反映另一个对象中的这些更改。由于我们想创建对象的副本,所以这个目的不会被浅拷贝来填补。
注意: C++ 编译器隐式地创建了一个复制构造函数并重载赋值运算符,以便在编译时执行浅复制。
如果在堆内存中定义了一些变量,则对象的浅拷贝,则:
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Box Class
class box {
private:
int length;
int breadth;
int height;
public:
// Function that sets the dimensions
void set_dimensions(int length1, int breadth1,
int height1)
{
length = length1;
breadth = breadth1;
height = height1;
}
// Function to display the dimensions
// of the Box object
void show_data()
{
cout << " Length = " << length
<< "\n Breadth = " << breadth
<< "\n Height = " << height
<< endl;
}
};
// Driver Code
int main()
{
// Object of class Box
box B1, B3;
// Set dimensions of Box B1
B1.set_dimensions(14, 12, 16);
B1.show_data();
// When copying the data of object
// at the time of initialization
// then copy is made through
// COPY CONSTRUCTOR
box B2 = B1;
B2.show_data();
// When copying the data of object
// after initialization then the
// copy is done through DEFAULT
// ASSIGNMENT OPERATOR
B3 = B1;
B3.show_data();
return 0;
}
C++
// C++ program to implement the
// deep copy
#include
using namespace std;
// Box Class
class box {
private:
int length;
int* breadth;
int height;
public:
// Constructor
box()
{
breadth = new int;
}
// Function to set the dimensions
// of the Box
void set_dimension(int len, int brea,
int heig)
{
length = len;
*breadth = brea;
height = heig;
}
// Function to show the dimensions
// of the Box
void show_data()
{
cout << " Length = " << length
<< "\n Breadth = " << *breadth
<< "\n Height = " << height
<< endl;
}
// Parameterized Constructors for
// for implementing deep copy
box(box& sample)
{
length = sample.length;
breadth = new int;
*breadth = *(sample.breadth);
height = sample.height;
}
// Destructors
~box()
{
delete breadth;
}
};
// Driver Code
int main()
{
// Object of class first
box first;
// Set the dimensions
first.set_dimension(12, 14, 16);
// Display the dimensions
first.show_data();
// When the data will be copied then
// all the resources will also get
// allocated to the new object
box second = first;
// Display the dimensions
second.show_data();
return 0;
}
输出:
Length = 14
Breadth = 12
Height = 16
Length = 14
Breadth = 12
Height = 16
Length = 14
Breadth = 12
Height = 16
深拷贝:
在Deep copy中,通过复制所有变量的数据来创建一个对象,并为该对象分配具有相同值的类似内存资源。为了执行深度复制,我们需要明确定义复制构造函数并在需要时分配动态内存。此外,还需要为其他构造函数中的变量动态分配内存。
下面是上述方法的实现:
C++
// C++ program to implement the
// deep copy
#include
using namespace std;
// Box Class
class box {
private:
int length;
int* breadth;
int height;
public:
// Constructor
box()
{
breadth = new int;
}
// Function to set the dimensions
// of the Box
void set_dimension(int len, int brea,
int heig)
{
length = len;
*breadth = brea;
height = heig;
}
// Function to show the dimensions
// of the Box
void show_data()
{
cout << " Length = " << length
<< "\n Breadth = " << *breadth
<< "\n Height = " << height
<< endl;
}
// Parameterized Constructors for
// for implementing deep copy
box(box& sample)
{
length = sample.length;
breadth = new int;
*breadth = *(sample.breadth);
height = sample.height;
}
// Destructors
~box()
{
delete breadth;
}
};
// Driver Code
int main()
{
// Object of class first
box first;
// Set the dimensions
first.set_dimension(12, 14, 16);
// Display the dimensions
first.show_data();
// When the data will be copied then
// all the resources will also get
// allocated to the new object
box second = first;
// Display the dimensions
second.show_data();
return 0;
}
输出:
Length = 12
Breadth = 14
Height = 16
Length = 12
Breadth = 14
Height = 16
想要从精选的视频和练习题中学习,请查看C++ 基础课程,从基础到高级 C++ 和C++ STL 课程,了解基础加 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程。