现代C++学习笔记——第1章 迈向现代C++

第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;
}

运行结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值