第1章迈向现代C++
本章分为两节
1.1 介绍新版的C++弃用的特性,在使用新标准的开发过程中,应该避免使用这些弃用的特,同时有的已经不支持了
比如,现在的C++中,char *str = "const text";这个在C++中已经不支持了,之前使用这种定义常量时就报错。
error: C2440: “初始化”: 无法从“const char [11]”转换为“char *”
1.2关于与C的兼容性
在与C语言进行混合编程时,使用#ifdef __cplusplus来控制调用C的函数。
例如:
//foo.h
#ifndef FOO_H
#define FOO_H
#ifdef __cplusplus
extern "C" {
#endif
int add(int x, int y);
#ifdef __cplusplus
}
#endif
#endif // FOO_H
//foo.c
#include "foo.h"
// C code
int add(int x, int y)
{
return x + y;
}
//main.cpp
#include "foo.h"
#include <iostream>
#include <functional>
using namespace std;
int main()
{
// use lambda expression
[out = std::ref(std::cout << "Result from C code: " << add(3, 5))]()
{
out.get() << ".\n";
}();
cout << "Hello World!" << endl;
return 0;
}
运行结果: