通常,创建对象的副本意味着创建具有相同字面量值,数据类型和资源的对象的精确副本。
- 复制构造函数
- 默认赋值运算符
// 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
深度复制:
在深层复制中,通过复制所有变量的数据来创建对象,并且该对象还将具有相同值的相似内存资源分配给该对象。为了执行深度复制,我们需要显式定义复制构造函数,并在需要时也分配动态内存。另外,还需要动态地将内存分配给其他构造函数中的变量。
下面是上述方法的实现:
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等的更多准备工作,请参阅“完整面试准备课程” 。