美文网首页C/C++
C++11关键字delete

C++11关键字delete

作者: cx7 | 来源:发表于2018-09-06 00:28 被阅读0次

delete

delete的原用法是用于释放new申请的内存

int *cacheMem = new int;
delete cacheMem;

int *cacheMem = new int[10];
delete[] cacheMem;

C++11标准对delete做了扩展 可以用于指示编译器删除不需要的函数生成
意味着可以做到:
1. 删除某种类成员函数 
2. 删除某种模版类型函数

删除指定的函数

可以删除类的某个成员函数 比如默认生成的构造函数

类默认生成的无参构造函数经常造成这个问题

class test {
public:
     test() {}
};

test ctx(); //返回类型为test的无参函数ctx
ctx并非是test类型的对象 反而是一个函数

屏蔽掉无参数函数或者改用{}来声明都可以解决这个问题
test ctx; //ctx是test类型的对象
test ctx{}; //ctx是test类型的对象

或者修改
class test {
public:
     test() {} = delete;
};

test ctx(); //ctx是test类型的对象

删除某种类型的模版函数

template <typename T>
void printVal(T &t);
void printVal(Char t) = delete; //被删除 当别处调用传入char类型参数时 编译器会报错

相关文章

  • c11的基础知识

    2020.04.141、c++11的default和delete关键字https://blog.csdn.net/...

  • C++11关键字delete

    delete 删除指定的函数 删除某种类型的模版函数

  • 2018-10-06

    C++11新关键字default C++11添加了很多新特性,使C++看上去更加的灵活和面向对象,其中新关键字de...

  • JavaScript进阶之:十、delete关键字

    delete关键字的作用:删除对象的属性 语法:delete 对象.属性可以删除没有使用var关键字声明的全局变量...

  • C++ 11 新特性

    C++11 中值得关注的几大变化(详解) | | 酷 壳 - CoolShell 1. delete 和 defa...

  • C++11 =default 和 =delete

    C++11 =default 和 =delete 1. = default 1.1 引入背景 C++ 的类有四类特...

  • C++11中auto和decltype

    C++11中auto和decltype auto和decltype都是C++11中引进来用于自动推断类型的关键字,...

  • 阿里巴巴面试题基础篇 C++11

    ● 请问C++11有哪些新特性? 参考回答: C++11 最常用的新特性如下: auto关键字:编译器可以根据初始...

  • ECMAScript

    ECMAScript关键字 delete do else finally function in instance...

  • C++11/14 constexpr 用法

    constexpr是C++11开始提出的关键字,其意义与14版本有一些区别。C++11中的constexpr指定的...

网友评论

    本文标题:C++11关键字delete

    本文链接:https://www.haomeiwen.com/subject/hqqawftx.html