特殊成员函数与三法则(Rule of Three)
• 四大特殊成员函数
• 默认构造函数(无参), 如果不定义任何拷贝构造,编译器自动生成
• 析构函数/ 拷贝构造函数/ 赋值操作符,如果不定义,编译器自动生成
• 使用default 让编译器自动生成。
• 使用delete 让编译器不要自动生成。
• 三法则:析构函数、拷贝构造函数、赋值操作符三者自定义其一,则需要同时定义另外两个,特别是有堆对象指针成员(编译器自动生成的一般语义错误)。(C++98)
• 编译器自动生成的拷贝/赋值是按字节拷贝,如不正确,则需要自定义拷贝/赋值/析构行为:
• 赋值操作符中的Copy & Swap 惯用法
• 注意赋值操作中避免“自我赋值”。
• 需要自定义三大函数的类,通常包含指针指向的动态数据成员。
-----------------------情况1------------------------------
class MyClass
{
public:
int x;
int y;
};
对于上面这样的空类,编译器会自动为该类添加 默认构造函数,拷贝构造,赋值操作符,默认析构函数,如下
class MyClass
{
public:
MyClass(){}
~MyClass(){}
MyClass(const MyClass& p)
{
...
..
}
MyClass & operator=(const MyClass& p)
{
...
..
}
int x;
int y;
};
//对于只有基础数据类型的类,编译器自动加的这4个函数没有问题,可以使用
----------------------------------------------------------
-------------------------情况2-----------------------------
稍微复杂些情况
public class Point
{
public:
int x;
int y;
};
public class Rectangle
{
public:
Point left;
int width;
int height;
}
public class MyClass
{
public:
Rectangle r;
int data;
}
//对于上面的情况, MyClass内嵌了对象类型的类和基础数据类型, 编译器自动生成的构造函数,拷贝构
//造,赋值操作符,析构函数,也能够直接使用.
-----------------------------------------------------
----------------------------------------------------
public class Point
{
public:
int x;
int y;
};
public class Rectangle
{
public:
Point* left;
int width;
int height;
} ;
//但是对于上面的Rectangle 中还有指针类型的情况,
//编译器自动生成的构造函数,拷贝构造,赋值操作符析构函数,就无法直接使用了这个时候需要自己重写
//改造如下
public class Rectangle
{
public:
Rectangle(int x, int y, int w_,int h_):
left(new Point(x,y)),
width(w_)
height(h_)
{
}
Rectangle (const Rectangle & r):
left(new Point(* r.left)),
width(r.width),
height(r.height)
{
// left(new Point(* r.left)) 这个会调用Point 的拷贝构造函数构造出一个新的Point堆对象
cout<<"拷贝构造";
}
Rectangle & operator=(const Rectangle & r)
{
if(this==&r){return *this;}
this->width = r.width;
this->height=r.height;
if(r.left==nullptr)
{
delete this->left;
this->left=nullptr;
}
else{
if(this->left!=nullptr)
{
*thi->left =* r.left; //此处会调用Point对象的赋值操作符
}
else
{
this->left=new Point(*r.left);
}
}
}
~Rectangle()
{
delete left;
left=nullptr;
}
Point* left;
int width;
int height;
} ;
//上面的方式中有一个不方便的地方就是,在赋值操作符中对空指针的判断显得比较繁琐,如果有多个指针的话
// 代码看起来会很繁琐,下面可以通过copy-swap 习惯法来完善
public class Point
{
public:
int x;
int y;
};
public class Rectangle
{
public:
//构造函数
Rectangle(int x, int y, int w_,int h_):
left(new Point(x,y)),
width(w_)
height(h_)
{
}
Rectangle (const Rectangle & r):
width(r.width),
height(r.height)
{
if(r.left!=nullptr)
{
left = new Point(* r.left);
}
else{
left=nullptr;
}
cout<<"拷贝构造";
}
Rectangle & operator=(const Rectangle & r)
{
if(this==&r){return *this;}
Rectangle temp(r) ; // 使用 Rectangle 拷贝构造函数创建一个局部对象
swap(this->left, temp.left); // 交换了 当前对象的left指针和 temp对象的 left指针
this->width = temp.width;
this->height=temp.height;
//出了作用域,temp栈对象销毁,
// Rectanlge里面的指针也会自动销毁(Swap后,temp对象里的这些指针是原来Rectangle对象的,本来需要手动delete,现在不需要手动delete)
}
~Rectangle()
{
delete left;
left=nullptr;
}
Point* left;
int width;
int height;
} ;
-------------------------------------------------------------------
public class Point
{
public:
int x;
int y;
};
public class Rectangle
{
public:
Rectangle() =default;
~Rectangle()=default;
Rectangle(const Rectangle & r)=default;
Rectangle & operator=(const Rectangle & r)=default;
Point left;
int width;
int height;
string name;
} ;
// Rectangle()=default; 表示无参默认的构造函数将会由编译器自动生成,自动生成的构造函数会自动的调用 Point 的无参默认的构造函数构造 left对象,会自动调用Rectangle其他所有成员默认构造函数做初始化
//同理Rectangle(const Rectangle & r)=default; 拷贝构造函数也由编译器自动合成,
//自动合成的拷贝构造函数会自动调用Point对象的拷贝构造函数构造对象,同时还会自动调用Rectangle所有其他成员拷贝构造函数做实例化
//如果 Rectangle(const Rectangle & r)=delete 表示编译器不会合成拷贝构造函数
---------------------------------------------
// 好的代码风格应该 显示化的告诉编译器 要不要自动合成 默认无参构造函数, 拷贝构造函数, 赋值操作符函数 如下
// Rectangle()=default;
// ~Rectangle()=default;
//Rectangle(const Rectangle & r)=default;
//Rectangle & operator=(const Rectangle & r)=default;
-------------------------------------------------
清楚对象的构造/析构点
• 构造器什么时候被调用?
• 当对象(包括嵌套成员)被定义时(堆栈、堆或静态)。
• 当对象的数组被定义时(堆栈、堆或静态)。
• 当函数参数以值传递时
• 当函数返回一个对象时
• 甚至适用于编译器生成的临时对象。
• 析构器什么时候被调用?
• 当命名的堆栈对象、数组或参数超出范围时(包括嵌套对象成员)
• 当堆对象或数组被删除时
• 对于静态对象,在程序结束时
• 对于临时对象,在创建它们的"完整表达式"结束时调用