c++0x11新特性:delete删除函数
c_plus_plus_0x11.cpp:
// c_plus_plus_0x11.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <Windows.h>
#include <iostream>
using namespace std; class A
{
public:
int add(int a, int b){ return a + b; }
int sub(int a, int b){ return a - b; }
}; class B :public A
{
public:
//int sub(int a, int b) = delete;
}; int _tmain(int argc, _TCHAR* argv[])
{
A a;
cout << "5+2=" << a.add(, ) << endl;
cout << "5-2=" << a.sub(, ) << endl<<endl; B b;
cout << "9+3=" << b.add(, ) << endl;
cout << "9-3=" << b.sub(, ) << endl << endl; cout << "done" << endl;
getchar();
return ;
}
运行效果:

把此行代码注析删除:
//int sub(int a, int b) = delete;
重新编译,结果为:

可见,delete可以用来删除一个类从基类继承的函数,听说可以用来删除拷贝构造函数,下面再尝试一下。
c_plus_plus_0x11.cpp:
// c_plus_plus_0x11.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <Windows.h>
#include <iostream>
using namespace std; class A
{
public:
A(int a, int b){ a_ = a; b_ = b; }
int add(){ return a_ + b_; }
int sub(){ return a_ - b_; } //A(const A&) = delete; // no copy int a_{};
int b_{};
}; int _tmain(int argc, _TCHAR* argv[])
{
A a(,);
cout << "5+2=" << a.add() << endl;
cout << "5-2=" << a.sub() << endl<<endl; A b=a;
cout << b.a_ << "+" << b.b_ << "=" << b.add() << endl;
cout << b.a_ << "-" << b.b_ << "=" << b.sub() << endl; cout << "done" << endl;
getchar();
return ;
}
运行效果:

把此行代码注析删除:
//A(const A&) = delete; // no copy
重新编译:

估计delete可以用来删除一个类从基类继承(如A.sub函数)的和此类隐式存在(如拷贝构造函数)的函数。
资料来源:
C++11 FAQ http://www.stroustrup.com/C++11FAQ.html#default
完。
c++0x11新特性:delete删除函数的更多相关文章
- ES6新特性之生成器函数 (generator function): function*
一.什么是生成器函数(generator function)? 生成器函数是ES6的新特性之一,它是一个在执行时能中途暂时退出,后面重新调用又能重新进入继续执行的一种函数. 并且在函数内定义的变量的所 ...
- ES6新特性:Function函数扩展, 扩展到看不懂
本文所有Demo的运行环境为nodeJS, 参考:让nodeJS支持ES6的词法----babel的安装和使用 : 函数的默认值: 如果有参数 ,那就用参数, 如果没有参数, 那就用默认的参数: aj ...
- ES6新特性3:函数的扩展
本文摘自ECMAScript6入门,转载请注明出处. 一.函数参数默认值 1. ES6允许为函数的参数设置默认值,即直接写在参数定义的后面. function log(x, y = 'World') ...
- vue3.x新特性之setup函数,看完就会用了
最近有小伙伴跟我聊起setup函数,因为习惯了vue2.x的写法导致了,setup用起来觉得奇奇怪怪的,在一些api混编的情况下,代码变得更加混乱了,个人觉得在工程化思想比较强的团队中使用setup确 ...
- Xcode6新特性(1)-删除Main.storyboard
当新建完一个空项目的时候,Xcode会自动创建一个Main.storyboard的空文件,如果不需要,可以将其删除.但是如果删除,再次运行程序,程序会报错,提示找不到Main.storyboard文件 ...
- Oracle11.2新特性之listagg函数 (行列转换)
SELECT regexp_substr('公司1,贵公司2', '[^,]+', 1, LEVEL, 'i') FROM dualCONNECT BY LEVEL <= length('公司1 ...
- C++11新特性:Lambda函数(匿名函数)
声明:本文参考了Alex Allain的文章http://www.cprogramming.com/c++11/c++11-lambda-closures.html 加入了自己的理解,不是简单的翻译 ...
- es6新特性之箭头函数
<script> { // es3,es5 var evens = [1, 2, 3, 4, 5]; var odds = evens.map(function (v) { return ...
- ES6新特性之箭头函数与function的区别
写法不同 // function的写法 function fn(a, b){ return a+b; } // 箭头函数的写法 let foo = (a, b) =>{ return a + b ...
随机推荐
- LeetCode208:Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...
- jQuery - 当当网我的订单页
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- oc50--@class1
// // main.m #import <Foundation/Foundation.h> #import "Person.h" int main(int argc, ...
- [整理] C#调用SQLDMO.DLL时间数据库备份 / 还原。 (香神无涯) // C#实现SQLSERVER2000数据库备份还原的两种方法 (带进度条)
/// <summary>/// 通过调用MSSQL的SQLDMO.DLL文件来实现备份数据库/// 1.首先在在项目中引用SQLDMO.DLL文件./// 2.在引用中的SQLDMO.D ...
- FreeWheel基于Go的实践经验漫谈——GC是大坑(关键业务场景不用),web框架尚未统一,和c++性能相比难说
摘自:http://www.infoq.com/cn/news/2017/06/freewheel-experience-on-go Go语言是FreeWheel公司目前主要力推的一个方向,在其看来, ...
- myeclipse视图布局恢复
使用Windows 菜单下的 Reset Perspective
- PCB MS SQL 将字符串分割为表变量(表值函数)
Create FUNCTION [dbo].[SplitTable]( @s varchar(max), --待分拆的字符串 ) --数据分隔符 ),), col varchar(max)) --建立 ...
- Django day08 多表操作 (四) 一对多, 多对多连续跨表查询
一对多 # 基于双下划线的一对多查询 # 查询出版社为上海出版社的所有图书 # ret = Publish.objects.filter(name='上海出版社').values('book__nam ...
- jdbc 接口学习笔记
一.JDBC概念 JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系型数据库提供统一访问,它由一组用Jav ...
- Django:提交表单报错:RuntimeError: You called this URL via POST, but the URL doesn’t end in a slash and you have A
Django:提交表单报错:RuntimeError: You called this URL via POST, but the URL doesn’t end in a slash and you ...