13.1

拷贝构造函数:这样的一个构造函数,第一个参数是自身类类型的引用,且任何额外参数都有默认值

使用拷贝初始化的情况:

  • 用=定义变量
  • 将一个对象作为实参传递给一个非引用类型的形参
  • 从一个返回类型为非引用类型的函数返回一个对象
  • 用花括号列表初始化一个数组中的元素或一个聚合类中的成员
  • 某些类类型对它们所分配的对象使用拷贝初始化(如初始化标准库容器或调用其insert/push成员时)

 

13.2

这是类Sales_data的拷贝构造函数,我们在调用该拷贝构造函数来初始化类对象时,需要拷贝它的实参,但为了拷贝实参,我们又需要调用拷贝构造函数,如此无限循环。

举例:

当我们调用拷贝构造函数(即Sales_data(Sales_data rhs);)构建对象时,如:

    Sales_data s1;
    Sales_data s2(s1);    //死循环

代码首先使用s1作为实参构建rhs,然后为了构建形参rhs,使用传递给rhs的参数(s1)来构建rhs调用的构造函数的形参,然后为了构建.....

所以该函数一直在构建这个构造函数的形参,而始终都没有进入函数体

 

13.3

复制StrBlob时,其成员data关联的计数器会加1

复制StrBlobPtr时,由于成员是weak_ptr,弱指针复制不影响计数器

 

13.4

使用拷贝构造函数的地方:

Point global;  
Pint foo_bar(Point arg)			//1  arg  
{               				//2  local  
    Point local = arg, *heap = new Point(global);		//3  heap指向的对象  
    *heap = local;  
    Point pa[4]={ local, *heap };						//4 5  pa[0] pa[1]  
    return *heap;										//6  函数返回值  
}

 

13.5

class HasPtr  
{  
    string *ps;  
    int i;  
public:  
    HasPtr(const string &s = string()): ps(new string(s)), i(0)	{}
    //*的优先级低于.的优先级 
	HasPtr(HasPtr &hp) : ps(new string(*hp.ps)), i(hp.i)	{}   
};

 

13.6

拷贝赋值运算符:一个名为 operator= 的函数

什么时候使用:类对象与类对象之间的赋值时

合成拷贝赋值运算符完成的工作:将右侧运算对象的每个非static成员赋予左侧运算对象的对应成员

什么时候生成合成拷贝赋值运算符:该类未定义自己的拷贝赋值运算符时

 

13.7

所有成员的赋值会发生。两个StrBlob中智能指针所指对象内存相同,计数器加一;两个StrBlobPtr中弱指针所指对象内存相同,计数器不变。

 

13.8

class HasPtr  
{  
    string *ps;  
    int i;  
public:  
    HasPtr(const string &s = string()): ps(new string(s)), i(0)	{}
	HasPtr(HasPtr &hp) : ps(new string(*hp.ps)), i(hp.i)	{}   
	HasPtr& operator=(const HasPtr &hp)
	{
		delete ps;
		ps = new string(*hp.ps);
		i = hp.i;
		return *this;
	}
};

 

13.9

析构函数:是类的一个成员函数,函数名为~类名,没有返回值,也不接受参数

合成析构函数完成的工作:对于某些类,它被用来阻止该类型的对象被销毁;但更多的是用来释放对象使用的资源并销毁对象的非static成员

什么时候生成:该类未定义自己的析构函数时

 

13.10

调用StrBlob的合成析构函数释放数据成员,shared_ptr调用自己的析构函数,使计数-1

调用StrBlobPtr的合成析构函数,weak_ptr调用自己的析构函数,计数器不变

 

13.11

class HasPtr  
{  
    string *ps;  
    int i;  
public:  
    HasPtr(const string &s = string()): ps(new string(s)), i(0)	{}
	HasPtr(HasPtr &hp) : ps(new string(*hp.ps)), i(hp.i)	{}   
	HasPtr& operator=(const HasPtr &hp)
	{
		delete ps;
		ps = new string(*hp.ps);
		i = hp.i;
		return *this;
	}
	~HasPtr()
	{
		delete ps;	
	}
};

 

13.12

3次,形参accum,局部对象item1、item2在离开作用域后调用自己的析构函数

trans为指向一个对象的指针,其离开作用域时,析构函数不会执行

 

13.13

 1 #include <iostream>
 2 #include <fstream>
 3 #include <sstream>
 4 #include <iterator>
 5 #include <initializer_list>
 6 #include <vector> 
 7 #include <string>
 8 #include <cstring>
 9 #include <deque>
10 #include <list> 
11 #include <forward_list>
12 #include <array>
13 #include <stack>
14 #include <queue>
15 #include <algorithm> 
16 #include <functional>
17 #include <map>
18 #include <set> 
19 #include <cctype>
20 #include <unordered_map>
21 #include <unordered_set>
22 #include <memory> 
23 #include <new> 
24  
25 using namespace std;
26 using namespace std::placeholders;
27 
28 struct X{
29     X() { cout << "X()" << endl; }
30     X(const X &x) {    cout << "X(const X &x)" << endl; }
31     X& operator=(const X &x)
32     {
33         cout << "operator=(const X &x)" << endl;
34         return *this;
35     }
36     ~X(){ cout << "~x()" << endl; } 
37 };
38 
39 int main()
40 {
41     //默认构造函数 
42     X x1;
43     X *p = new X();
44     //拷贝构造函数 
45     X x2(x1);
46     X x3 = x1;
47     X *q = new X(x1); 
48     //拷贝赋值运算符 
49     x3 = x2;
50     //析构函数 
51     delete p;
52     delete q;
53     return 0;
54 }
View Code

相关文章:

  • 2022-01-27
  • 2021-07-13
  • 2021-09-06
  • 2021-08-19
  • 2021-12-04
  • 2022-12-23
  • 2022-12-23
  • 2021-06-17
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-05
  • 2022-12-23
  • 2019-08-29
  • 2019-09-24
相关资源
相似解决方案