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. 百练-16年9月推免-B题-字符串判等

    2743:字符串判等 查看 提交 统计 提示 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 判断两个由大小写字母和空格组成的字符串在忽略大小写,且忽略空格后是否相等. 输入 ...

  2. 【HDOJ3567】【预处理bfs+映射+康拓展开hash】

    http://acm.hdu.edu.cn/showproblem.php?pid=3567 Eight II Time Limit: 4000/2000 MS (Java/Others)    Me ...

  3. eclipse项目版本控制忽略上传文件

    *.classpath *.project */.git/* .deployables .git .settings .svn _svn bin target

  4. 【mybatis源码学习】mybtias一级,二级缓存

    转载:https://www.cnblogs.com/ysocean/p/7342498.html mybatis 为我们提供了一级缓存和二级缓存,可以通过下图来理解: ①.一级缓存是SqlSessi ...

  5. 比较器(TreeSet和优先队列,可以对里面的元素按照自己的意愿进行排序)

    class MyComparator implements Comparator<Integer>{ public int compare(Integer o1, Integer o2) ...

  6. wget命令的使用

    wget是在命令行下载文件的命令 -c 断点续传,用于大文件的下载或者网络不稳定的情况下,一般不写也可以 -P 指定下载到那个目录(是大写的P) 举例 [root@bogon local]# wget ...

  7. mino federation 功能

    mino federation 可以让我们进行 bucket的查找,实现更灵活的分布式控制 其中依赖了几个组件 coredns etcd 参考架构图 说明 官方也提供了一个简单的部署的demo,总的来 ...

  8. Using gcc stack debug skill

    The stack error is hard to debug, but we can debug it assisted by the tool provided by GCC. As we kn ...

  9. python基础教程_学习笔记9:抽象

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/signjing/article/details/30745465 抽象 懒惰即美德. 抽象和结构 抽 ...

  10. java_oop_类与对象

    类与对象包方法 ========================================= 对象的概念类与对象的关系    万物皆对象        属性(名词)            对象的 ...