現代C++學習筆記——第1章
阿新 • • 發佈:2021-01-12
技術標籤:# Modern C++學習筆記Modern 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 #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"
int add(int x, int y)
{
return x + y;
}
//main.cpp #include "foo.h" #include <iostream> #include <functional> int main() { std::cout << "Modern CPP" << std::endl; [out = std::ref(std::cout << "Result from C code:" << add(1, 2))](){ out.get() << ".\n"; }(); return 0; }
執行結果: