C++~07补充

1. nullptr(掌握)

在传统的C和C++中,空指针使用的关键字是NULL,但是存在下面的问题:

复制代码
#include <iostream>`

`using` `namespace std;`

`void` `func(int i)`
`{`
`    cout <<` `"A"` `<< i << endl;`
`}`

`void` `func(char* i)`
`{`
`    cout <<` `"B"` `<< i << endl;`
`}`


`int` `main()`
`{`
    `func(NULL);` `// A0`

    `return` `0;`
`}`
`

在C++11中,使用新的关键字nullptr代替NULL,可以解决上述问题:

复制代码
#include <iostream>`

`using` `namespace std;`

`void` `func(int i)`
`{`
`    cout <<` `"A"` `<< i << endl;`
`}`

`void` `func(char* i)`
`{`
`    cout <<` `"B"` `<< i << endl;`
`}`


`int` `main()`
`{`
    `func(nullptr);` `// B`

    `return` `0;`
`}`
`

2. 类型推导(熟悉)

使用关键字auto可以自动推导变量类型。

复制代码
#include <iostream>`

`using namespace std;`


`int main()`
`{`
`    auto i = 5; // auto会被自动推导为int`
`    cout << i << endl;`
`    auto i2 = new auto(5); // auto会被自动推导为int*`
`    cout << i2 << " " << *i2 << endl;`
`    delete i2;`

`    return 0;`
`}`
`

C++11中引入了decltype关键字,可以推导表达式。推导的过程中只推导类型计算,不进行数值计算。

复制代码
#include <iostream>`

`using namespace std;`


`int main()`
`{`
`    auto i = 5;`
`    auto i2 = 5.4;`

`    decltype(i+i2) i3 = 3.14; // 推导表示的类型作为i3的类型`
`    cout << i3 << endl;`

`    return 0;`
`}`
`

3. 至此C++你已基本会用了,那么扪心自问你都学会了吗(重点)

C++11新特性你了解哪些?

  1. 智能指针

  2. nullptr

  3. 类型推导

  4. 类型转换

  5. 继承构造

  6. array

  7. override

  8. 初始化构造、窄化

  9. for-each(增强型for循环)

10.constexpr

......

如果没有难到你恭喜你已具备现代C++的基本编程能力了。

相关推荐
csdn_aspnet1 小时前
C++ 使总距离最短的最佳点位置(Optimum location of point to minimize total distance)
c++·算法
代码民工正电荷3 小时前
JAVA重要知识点和集合
java·开发语言
星恒随风3 小时前
C++11 加餐(4)并发支持库——condition_variable 条件变量与生产者消费者模型
开发语言·c++
利威尔xu3 小时前
Go map 并发十连问:多 goroutine 同时读写触发 fatal error,进程直接崩溃
开发语言·golang
你不知道我是谁?3 小时前
GESP C++ 补充知识:浮点数格式化输出(%f/%e/%g)与 fmod 浮点余数实战
开发语言·c++
九蝶科技4 小时前
运费分摊的二开实现:BigDecimal 精确分摊 + 尾差补齐 + 暂估调整
开发语言·python
Ryan-Zhong4 小时前
一次接入 16 个平台:视频链接元数据解析 API 的 Python 实战与踩坑记录
开发语言·python·dreamweaver
Rabitebla5 小时前
【Linux】 文件系统(五):挂载与软硬链接,多个文件系统怎样组成统一目录树
linux·运维·服务器·c++·笔记·学习·算法
无限聚核6 小时前
PHP 对接美团、抖音团购核销接口:预核销、核销、重试和撤销一份代码跑通,无限聚核
开发语言·php·美团·团购核销接口
WL学习笔记7 小时前
继承(C++)
c++·继承