20分钟了解C++11

9 override关键字 (虚函数使用)

 *
* 避免在派生类中意外地生成新函数
*/
// C++ 03
class Dog {
virtual void A(int);
virtual void B() const;
} class Yellowdog : public Dog {
virtual void A(float); // 生成一个新函数
virtual void B(); //生成一个新函数
} // C++ 11
class Dog {
virtual void A(int);
virtual void B() const;
void C();
} class Yellowdog : public Dog {
virtual void A(float) override; // Error: no function to override
virtual void B() override; // Error: no function to override
void C() override; // Error: not a virtual function
}

10 final (虚函数和类使用)


class Dog final { // 在类名后添加表示不能再从该类派生子类
...
}; class Dog {
virtual void bark() final; // 在虚函数后添加表示该函数不能再被覆写
};

11 编译器生成默认构造函数

class Dog {
Dog(int age) {}
}; Dog d1; // 错误:编译器不会自动生成默认构造函数,因为已经有构造函数了 // C++ 11:
class Dog {
Dog(int age);
Dog() = default; // 强制编译器生成默认构造函数
};

12 delete 禁用某些函数


class Dog {
Dog(int age) {}
} Dog a(2);
Dog b(3.0); // 3.0会从double类型转成int类型
a = b; // 编译器会生成赋值运算符 // C++ 11 手动禁用:
class Dog {
Dog(int age) {}
Dog(double ) = delete;
Dog& operator=(const Dog&) = delete;
}

13 constexpr 常量表达式

int arr[6];    //OK
int A() { return 3; }
int arr[A()+3]; // 编译错误 // C++ 11
constexpr int A() { return 3; } // 强制计算在编译时发生 int arr[A()+3]; // 生成一个大小为6的数组 // 利用constexpr写更快的程序
constexr int cubed(int x) { return x * x * x; } int y = cubed(1789); // 在编译时计算 //cubed()函数:
//1. 非常快,不消耗运行时计算
//2. 非常小,不占据二进制空间

14 新的字符串字面值

  // C++ 03:
char* a = "string"; // C++ 11:
char* a = u8"string"; // UTF-8 string.
char16_t* b = u"string"; // UTF-16 string.
char32_t* c = U"string"; // UTF-32 string.
char* d = R"string \\" // raw string.

15 lambda函数

cout << [](int x, int y){return x+y}(3,4) << endl;  // Output: 7
auto f = [](int x, int y) { return x+y; };
cout << f(3,4) << endl; // Output: 7 template<typename func>
void filter(func f, vector<int> arr) {
for (auto i: arr) {
if (f(i))
cout << i << " ";
}
} int main() {
vector<int> v = {1, 2, 3, 4, 5, 6 }; filter([](int x) {return (x>3);}, v); // Output: 4 5 6
...
filter([](int x) {return (x>2 && x<5);}, v); // Output: 3 4 int y = 4;
filter([&](int x) {return (x>y);}, v); // Output: 5 6
//注: [&] 告诉编译器要变量捕获
} // Lambda函数几乎像是一个语言的扩展,非常方便
// template
// for_nth_item

17 用户自定义的字面值


// C ++在很大程度上使用户定义的类型(类)与内置类型的行为相同.
// 用户自定义的字面值使其又往前迈出一步 //老的C++,不知道到底表示多长?
long double height = 3.4; // 在学校物理课的时候
height = 3.4cm;
ratio = 3.4cm / 2.1mm; // 为什么在编程时不这么做?
// 1. 语言不支持
// 2. 单位转换需要运行时开销 // C++ 11:
long double operator"" _cm(long double x) { return x * 10; }
long double operator"" _m(long double x) { return x * 1000; }
long double operator"" _mm(long double x) { return x; } int main() {
long double height = 3.4_cm;
cout << height << endl; // 34
cout << (height + 13.0_m) << endl; // 13034
cout << (130.0_mm / 13.0_m) << endl; // 0.01
} //注: 加constexpr使单位转换在编译时计算 // 限制: 只能用于以下参数类型:
char const*
unsigned long long
long double
char const*, std::size_t
wchar_t const*, std::size_t
char16_t const*, std::size_t
char32_t const*, std::size_t
// 返回值可以是任何类型 // 例子:
int operator"" _hex(char const* str, size_t l) {
// 将十六进制格式化字符串str转成int ret
return ret;
} int operator"" _oct(char const* str, size_t l) {
// 将格式化8进制字符串str转成 int ret
return ret;
} int main() {
cout << "FF"_hex << endl; // 255
cout << "40"_oct << endl; // 32
}

18 可变参数模板 Variadic Template

 *
* 可以接受任意个数的任意类型模板参数的模板
* 类和函数模板都可以是可变的
*/
template<typename... arg>
class BoTemplate; BoTemplate<float> t1;
BoTemplate<int, long, double, float> t2;
BoTemplate<int, std::vector<double>> t3; BoTemplate<> t4; // 可变参数和非可变参数组合
template<typename T, typename... arg>
class BoTemplate; BoTemplate<> t4; // Error
BoTemplate<int, long, double, float> t2; // OK // 定义一个默认模板参数
template<typename T = int, typename... arg>
class BoTemplate;

19 模板别名 Template Alias

  template<class T> class Dog { /* ... */ };
template<class T>
using DogVec = std::vector<T, Dog<T>>; DogVec<int> v; // 同: std::vector<int, Dog<int>>

20 decltype

 * 相当于GNU typeof
*/
const int& foo(); // 声明一个函数foo()
decltype(foo()) x1; // 类型是const int& struct S { double x; };
decltype(S::x) x2; // x2类型double auto s = make_shared<S>();
decltype(s->x) x3; // x3类型double int i;
decltype(i) x4; // x4类型int float f;
decltype(i + f) x5; // x5类型float // decltype对于模板泛型编程非常有用
template<type X, type Y>
void foo(X x, Y y) {
...
decltype(x+y) z;
...
} // 如果返回值要使用decltype?
template<type X, type Y>
decltype(x+y) goo(X x, Y y) { // 错误:x 和 y 未定义
return x + y;
} // auto和decltype组合实现具有尾随返回类型的模板
template<type X, type Y>
auto goo(X x, Y y) -> decltype(x+y) {
return x + y;
}

C++11--20分钟了解C++11 (下)的更多相关文章

  1. C++11--20分钟了解C++11 (上)

    20分钟了解C++ 11 1 初始化列表 Initializer List //C++ 03中用初始化列表初始化数组 int arr[4] = {3, 2, 4, 5}; vector<int& ...

  2. Java基础部分(11~20)

    11."=="和 equals 方法究竟有什么区别? (单独把一个东西说清楚,然后再说清楚另一个,这样,它们的区别自然就出来了,混在一起说,则很难说清楚) ==操作符专门用来比较两 ...

  3. 【转】Cocos2d-x 3.1.1 学习日志6--30分钟了解C++11新特性

    [转]Cocos2d-x 3.1.1 学习日志6--30分钟了解C++11新特性 Cocos2d-x 3.1.1 学习日志6--30分钟了解C++11新特性

  4. 【转载】20分钟MySQL基础入门

    原文:20分钟MySQL基础入门 这里持续更新修正 开始使用 MySQL 为关系型数据库(Relational Database Management System),一个关系型数据库由一个或数个表格 ...

  5. 20分钟成功编写bootstrap响应式页面 就这么简单

    最近发现一个叫 Bootstrap 的好东西,Bootstrap 是现在最流行的响应式 CSS 框架,它以移动设备优先,能够快速适应不同设备.使用它编写响应式页面快捷.方便,而且屏蔽了浏览器差异.使用 ...

  6. Visual Studio原生开发的20条调试技巧(下)

    我的上篇文章<Vistual Studio原生开发的10个调试技巧>引发了很多人的兴趣,所以我决定跟大家分享更多的调试技巧.接下来你又能看到一些对于原生应用程序的很有帮助的调试技巧(接着上 ...

  7. 无语的index hint:手工分配哈希区,5小时不出结果,优化后20分钟

    同事说,有个语句5个小时不出结果,叫我帮忙看看,于是叫同事发过来.不看不知道,一看吓一跳,3个表关联,强制使用了2个index hint,当中一个表9g,一个表67g,另一个小表40Mb.开发者,总以 ...

  8. 关于.net服务启动注册到zookeeper,但是注册节点20分钟自动消失解决办法

        ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,作用简单描述就是相当于一个中介,服务提供者将服务注册到zk,服务调用者直接从zk获取,zk的作用就是协调     最近碰到公 ...

  9. Hawk: 20分钟无编程抓取大众点评17万数据

    1. 主角出场:Hawk介绍 Hawk是沙漠之鹰开发的一款数据抓取和清洗工具,目前已经在Github开源.详细介绍可参考:http://www.cnblogs.com/buptzym/p/545419 ...

随机推荐

  1. poj-3666 【对dp子状态无后效性的理解】

    题目链接 错解: #include <cstdio> #include <cstring> #include <algorithm> #include <io ...

  2. SQLI DUMB SERIES-6

    less6 输入 ?id=1" 说明双引号未被过滤,输入的id被一对双引号所包围,存在注入点,可以闭合双引号. 而输入正常时 情况和less5相同,因此注入同less5.

  3. ACM C++

    杭电oj 2000 sort(a,a+sizof(a)); 对数组a中元素进行排序,参数1,2分别为要排序数组首末地址 故sort句换成下行也通过 sort(a,&a[3]); ac代码 // ...

  4. ajax的原生调用

    https://blog.csdn.net/jitianxia68/article/details/78393406

  5. 从简单的mongodb example 的观察

    https://github.com/no7dw/mongodb-example 这是最基础的连接查询.(branch master) var MongoClient = require('mongo ...

  6. nginx常用参数设置

    1)隐藏nginx header 版本号 使用curl -I http://www.10.0.3.46 会发现server那里显示版本号 在nginx.conf的http里添加参数server_tok ...

  7. Hive GenericUDF2

    再来看一个分数统计的小例子. 在Hive中存在如下一张表: hive> describe tb_test2; OK name    string score_list  array<map ...

  8. WebSocket-Over-HTTP Protocol

    转自:https://pushpin.org/docs/protocols/websocket-over-http/ WebSocket-Over-HTTP Protocol The WebSocke ...

  9. PowerDesigner V16.5 安装及汉化

    一.power designer是什么以及是干什么的? power designer是能进行数据库设计的强大的软件,是一款开发人员常用的数据库建模工具.分别从概念数据模型(Conceptual Dat ...

  10. ASP.NET MVC中常用的ActionResult类型

    常见的ActionResult 1.ViewResult 表示一个视图结果,它根据视图模板产生应答内容.对应得Controller方法为View. 2.PartialViewResult 表示一个部分 ...