默认拷贝构造函数的行为如下: 深拷贝和浅拷贝的定义可以简单理解成:如果一个类拥有资源(堆,或者是其它系统资源),当这个类的对象发生复制过程的时候(复制指针所指向的值),这个过程就可以叫做深拷贝,反之对象存在资源但复制过程并未复制资源(只复制了指针所指的地址)的情况视为浅拷贝。 浅:
using
namespace
std;
// shallow && deep copy // deep copy make pointer point to a new place! class Product ... { public:int* pointer; Product(int i=0) ...{ pointer=new int(i); } //change class variable void change(int i) ...{ *pointer=i; } //deconstructor ~Product() ...{ delete pointer; } } ; int main() ... { Product p1(2); Product p2(p1); p1.change(3); cout<<*p2.pointer<<endl; getchar(); return 0; }
深:
using
namespace
std;
// shallow && deep copy // deep copy make pointer point to a new place! class Product ... { public:int* pointer; Product(int i=0) ...{ pointer=new int(i); } //change class variable void change(int i) ...{ *pointer=i; } // copying constructor Product(const Product &p) ...{ pointer=new int(*p.pointer); } //deconstructor ~Product() ...{ delete pointer; } } ; int main() ... { Product p1(2); Product p2(p1); p1.change(3); cout<<*p2.pointer<<endl; getchar(); return 0; }
2 拷贝构造函数的另一种调用
#include
<
iostream
>
using namespace std; class Date ... { int n; public: Date(int i = 0) ...{ cout << "载入构造函数" << endl; n = i; } Date(const Date &d) ...{ cout << "载入拷贝构造函数" << endl; n = d.n; } int GetMember() ...{ return n; } } ; void Display(Date obj) // 针对obj的操作实际上是针对复制后的临时拷贝进行的 ... { cout << obj.GetMember() << endl; } int main() ... { Date a; Date b(99); Display(a); //对象直接作为参数 Display(b); //对象直接作为参数 getchar(); return 0; }
程序输出:
#include
<
iostream
>
using namespace std; class Date ... { int n; public: Date(int i = 0) ...{ cout << "载入构造函数" << endl; n = i; } Date(const Date &d) ...{ cout << "载入拷贝构造函数" << endl; n = d.n; } void Show() ...{ cout << "n = " << n << endl; } } ; Date GetClass( void ) // 函数中的局部对象被用作返回值,按理说应该引用拷贝构造函数 ... { Date temp(100); return temp; } int main() ... { Date a; a.Show(); a = GetClass();//这里GetClass()函数中的局部对象被用作返回值 a.Show(); Date b = GetClass();//这里GetClass()函数中的局部对象被用作返回值 b.Show(); getchar(); return 0; }
程序输出: 3.3 无名对象
#include
<
iostream
>
using namespace std; class Date ... { int n; public: Date(int i = 0) ...{ cout << "载入构造函数" << endl; n = i; } Date(const Date &d) ...{ cout << "载入拷贝构造函数" << endl; n = d.n; } void Show() ...{ cout << "n = " << n << endl; } } ; int main() ... { Date a(100); a.Show(); Date b = a; //"="在对象声明语句中,表示初始化,调用拷贝构造函数 b.Show(); Date c; c.Show(); c = a; //"="在赋值语句中,表示赋值操作,调用赋值函数 c.Show(); getchar(); return 0; }
程序输出: 上面代码的运行结果有点“出人意料”,从思维逻辑上说,当无名对象创建了后,是应该调用自定义拷贝构造函数,或者是默认拷贝构造函数来完成复制过程的,但事实上系统并没有这么做,因为无名对象使用过后在整个程序中就失去了作用。对于这种情况c++会把代码看成是: Internet a ("中国"); 省略了创建无名对象这一过程,所以说不会调用拷贝构造函数。 3.赋值符的重载 (1) 没有重载赋值函数
#include
"
stdafx.h
"
using namespace std; // shallow && deep copy // deep copy make pointer point to a new place! class Product ... { public:int* pointer; Product(int i=0) ...{ pointer=new int(i); } //change class variable void change(int i) ...{ *pointer=i; } // copying constructor Product(const Product &p) ...{ pointer=new int(*p.pointer); } //deconstructor ~Product() ...{ delete pointer; } } ; int main() ... { Product p1(1); Product p2(2); Product p3(3); p2=p3; p3.change(4); cout<<*p2.pointer<<endl; getchar(); return 0; } // 结果输出4
(2)重载赋值函数
#include
"
stdafx.h
"
using namespace std; // shallow && deep copy // deep copy make pointer point to a new place! class Product ... { public:int* pointer; Product(int i=0) ...{ pointer=new int(i); } //change class variable void change(int i) ...{ *pointer=i; } // copying constructor Product(const Product &p) ...{ pointer=new int(*p.pointer); } //重载= Product& operator=(const Product &p) ...{ if(this!=&p) pointer=new int(*p.pointer); return *this; } //deconstructor ~Product() ...{ delete pointer; } } ; int main() ... { Product p1(1); Product p2(2); Product p3(3); p2=p3; p3.change(4); cout<<*p2.pointer<<endl; getchar(); return 0; } // 输出3
5. 在拷贝构造函数中使用赋值函数
#include
<
iostream
>
using namespace std; class Date ... { int da, mo, yr; public: Date(int d = 0, int m = 0, int y = 0) ...{ cout << "载入构造函数" << endl; da = d; mo = m; yr = y; } Date(const Date &other); Date & operator =(const Date &other); void Show() ...{ cout << mo << "-" << da << "-" << yr << endl; } } ; Date::Date( const Date & other) // 拷贝构造函数中使用赋值函数 ... { cout << "载入拷贝构造函数" << endl; *this = other; } Date & Date:: operator = ( const Date & other) ... { cout << "载入赋值函数" << endl; if(this == &other) return *this; da = other.da; mo = other.mo; yr = other.yr; return *this; } int main() ... { Date a(1, 3, 6); a.Show(); Date b = a; b.Show(); Date c; c.Show(); c = a; c.Show(); getchar(); return 0; }
程序输出: 6. 偷懒的办法处理拷贝构造函数和赋值函数 |
C++拷贝构造函数
最新推荐文章于 2024-08-05 22:21:17 发布
关键字: C++