《Effective C++》中条款5到条款12是在讲述类中默认函数的一些书写“坑”,我总结一下,依照更为简便的Demo进行一个概括,并使用C++11的一些语法糖和新特性,更加容易的表现出这个注意事项。
贴一个简洁代码,可以直接下去看结论
#include<iostream>
using namespace std;
namespace D {
class Base {
public:
Base(int b1 = 0,int b2 = 0)
:_b1(b1),
_b2(b2),
_bP (make_shared<int>(10) )
{
cout << "基类构造" << "\n";
}
Base(const Base& copy)
:_b1(copy._b1),
_b2(copy._b2),
_bP(copy._bP)
{
cout << "基类拷贝构造 "<< "\n";
}
Base(const Base*copy)
:_b1(copy->_b1),
_b2(copy->_b2),
_bP(copy->_bP)
{
cout << "基类使用指针 拷贝构造 " << "\n";
}
Base& operator=(const Base& copy)
{
_b1 = copy._b1;
_b2 = copy._b2;
_bP = make_shared<int>(10);
cout<< "基类赋值拷贝构造 " << "\n";
return *this;
}
shared_ptr<int>& getPoint()
{
return this->_bP;
}
int _b1;
double _b2;
shared_ptr<int> _bP;
};