delete.override.委托构造函数
class Base {
public:
Base() {}
// 禁用拷贝构造函数
Base(Base const &other) = delete;
// 禁用赋值操作
Base& operator = (Base const &other) = delete;
virtual void func() {}
// 禁止派生类重写
virtual void fune() final {}
};
class deleGate: Base {
private:
int a;
std::string str;
public:
// 委托构造函数,防止递归委托
deleGate() {
str = "hao";
}
deleGate(int _a):deleGate() {
// err deleGate(int _a):deleGate(), a(_a) {
a = _a;
}
void func() override{}
//void fune() {} 无法重写
};
shared_ptr
c++11之后新增的智能指针,实现栈上对堆内存的控制。
多个线程同时读同一个shared_ptr对象是线程安全的,但是如果是多个线程对同一个shared_ptr对象进行读和写,则需要加锁。
// 推荐
std::shared_ptr<int> sp = std::make_shared<int>(10);
// 非推荐
std::shared_ptr<int> spEx = std::shared_ptr<int>(new int(0));
std::cout << *sp << sp.use_count() <<std::endl;
int *sper = sp.get();
使用情景:
当类Test被share_ptr管理,且在类Test的成员函数里需要把当前类对象作为参数传给其他函数时,就需要传递一个指向自身的share_ptr
class Test:public std::enable_shared_from_this<Test>
{
public:
Test()
{
std::cout << "Test()" << std::endl;
}
~Test()
{
std::cout << "~Test()" << std::endl;
}
std::shared_ptr<Test> getThis() {
return std::enable_shared_from_this<Test>::shared_from_this();
}
};
// err 出现多次析构
class Test
{
public:
std::shared_ptr<Test> getThis() {
return std::shared_ptr<Test>(this);
}
};
eg:
{
std::shared_ptr<Test> p = std::make_shared<Test>();
std::shared_ptr<Test> oT = p->getThis();
}
std::function
// 仿函数
class CusFunObj{
public:
CusFunObj(int _level):m_level(_level){}
bool operator ()(int other){
return other < m_level;
}
private:
int m_level;
};
// 类函数
class CusObj{
public:
bool classFun(int a){
return true;
}
};
// 一般函数
bool normalFun(int a){
return true;
}
// 伟大的std::function
std::function<bool(int)> funObj = nullptr;
// 一般函数
funObj = normalFun;
// lambda
funObj = [](int a){
return true;
};
// 成员函数
CusObj obj;
funObj = std::bind(&CusObj::classFun, &obj, std::placeholders::_1);
// 仿函数
funObj = CusFunObj(10);
强类型枚举
enum class CUS_TYPE{
TYPE_0 = 0,
TYPE_1,
};
enum class SPE_TYPE{
TYPE_1 = 0,
TYPE_0,
};
CUS_TYPE one = CUS_TYPE::TYPE_0;
if(one == CUS_TYPE::TYPE_0){}
// err
//if(one == 0)
以前的,枚举常量暴露在外层作用域中,这可能导致名字冲突【如果同一个作用域中存在两个不同的枚举类型,但是具有相同的枚举常量就会冲突】,转换时,被隐式转换为整形。
强类型的枚举,支持并强制转换特定的用户定义类型。
如上if(one == 0)由于没有可接受的转换,无法编译通过的。
待添加