cppm報(bào)名中心(cppreference)


本站AI自動(dòng)判斷提供您所需要的app下載:點(diǎn)我下載安裝,你懂的APP

隨著報(bào)考人數(shù)增多,很多學(xué)員錯(cuò)過(guò)班期

因此建議先預(yù)定全年名額,拿到試題、教材。

然后根據(jù)工作時(shí)間選擇合適班期

參加現(xiàn)場(chǎng)培訓(xùn)、考試。

9?月份

上海 9-11號(hào)培訓(xùn) 12號(hào)考試

深圳16-18號(hào)培訓(xùn)19號(hào)考試

北京23-25號(hào)培訓(xùn)26號(hào)考試

10?月份

廣州14-16號(hào)培訓(xùn)17號(hào)考試

無(wú)錫21-23號(hào)培訓(xùn)24號(hào)考試

濟(jì)南21-23號(hào)培訓(xùn)24號(hào)考試

杭州21-23號(hào)培訓(xùn)24號(hào)考試

成都28-30號(hào)培訓(xùn)31號(hào)考試

11?月份

北京11-14號(hào)培訓(xùn)15號(hào)考試

上海18-20號(hào)培訓(xùn)21號(hào)考試

深圳25-27號(hào)培訓(xùn)28號(hào)考試

12?月份

廣州 9-11號(hào)培訓(xùn)12號(hào)考試

杭州16-18號(hào)培訓(xùn)19號(hào)考試

成都23-25號(hào)培訓(xùn)26號(hào)考試

濟(jì)南23-25號(hào)培訓(xùn)26號(hào)考試

本文主要參考了C++標(biāo)準(zhǔn)委員會(huì)出品的 Mix-c-and-cpp 和 Learning C++ if you already know C 兩個(gè)鏈接對(duì)應(yīng)的一些問(wèn)答內(nèi)容,加上我個(gè)人的理解完成。本文先會(huì)說(shuō)明C++和C的發(fā)展歷史和它們之間關(guān)系,然后從編程范式上提綱挈領(lǐng)地總結(jié)一下表達(dá)下我個(gè)人的感受,之后從幾個(gè)區(qū)別點(diǎn)來(lái)具體說(shuō)明它們的不同。有不準(zhǔn)確或疏漏的地方還請(qǐng)大家指正,謝謝。

然后,關(guān)于C++語(yǔ)言標(biāo)準(zhǔn)參考文檔我推薦 cppreference c++ language document ;關(guān)于C語(yǔ)言標(biāo)準(zhǔn)參考文檔我推薦 cppreference c language document ;有興趣的朋友可以看看,當(dāng)然也可以把它們當(dāng)成參考文檔查閱用。

然后現(xiàn)在最新的C++標(biāo)準(zhǔn)是C++1x,C標(biāo)準(zhǔn)是C1x,希望大家都可以用最新的標(biāo)準(zhǔn),因?yàn)樽钚碌臉?biāo)準(zhǔn)填上了很多坑。另外,提醒一下,其實(shí)C++包括兩個(gè)部分:C++語(yǔ)言(language)部分和C++庫(kù)(library)部分,學(xué)C++這兩部分都要學(xué)好;學(xué)習(xí)C語(yǔ)言也是一樣。

本文的主要內(nèi)容包括四個(gè)部分。第一部分是 背景:C1x與C++1x是一對(duì)兄弟,C95是他們的爹 ,第二部分是 C/C++區(qū)別-從編程范式上來(lái)講 ,第三部分是 C/C++區(qū)別-重要的(區(qū)別0-9) , 第四部分是 C/C++區(qū)別-其他(區(qū)別10-12和其他) 。本文最末給出了一些相關(guān)的參考文章。

(一/四) 背景:C1x與C++1x是一對(duì)兄弟,C95是他們的爹


引用一下 Learning C++ if you already know C 中的一段話,概括一下C++1x/C1x和C95的關(guān)系。C++98和C99其實(shí)已經(jīng)有不兼容的地方了,那么在之后的進(jìn)程中這些不兼容的點(diǎn)會(huì)更多;因?yàn)槭聦?shí)上在語(yǔ)言層面上,C++新標(biāo)準(zhǔn)和C新標(biāo)準(zhǔn)制定的是不同語(yǔ)言的標(biāo)準(zhǔn),只不過(guò)它們的語(yǔ)法有許多相同或者相似的部分。然后下面引用的段落中的幾個(gè)鏈接為C++之父推薦看的文檔或書(shū)籍,大家感興趣的話,可以看一看這些資料。

C++ is a direct descendant of C95 (C90 plus an Amendment) that retains almost all of C95 as a subset. C++ provides stronger type checking than C and directly supports a wider range of programming styles than C. C++ is “a better C” in the sense that it supports the styles of programming done using C with better type checking and more notational support (without loss of efficiency). In the same sense, ANSI C90/C95 is a better C than K&R C. In addition, C++ supports data abstraction, object-oriented programming, and generic programming (see The C++ Programming Language; Appendix B discussing compatibility issues is available for downloading).

We have never seen a program that could be expressed better in C95 than in C++ (and we don’t think such a program could exist – every construct in C95 has an obvious C++ equivalent). However, there still exist a few environments where the support for C++ is so weak that there is an advantage to using C instead. There aren’t all that many of those left, though; see Stroustrup’s (incomplete) compilers list.

For a discussion of the design of C++ including a discussion of its relationship with C see The Design and Evolution of C++.

Please note that “C” in the paragraphs above refers to Classic C and C95 (C90 with an Amendment). C++ is not a descendant of C99; rather, both are derived from C95. C++11 adopted all of C99’s preprocessor extensions and library extensions, but not C99’s new language features, so language features like the restrict keyword that were added in C99 are generally not part of ISO C++. Here is a description of the differences between C++98 and C99.

(二/四) C/C++區(qū)別-從編程范式上來(lái)講


從編程范式上來(lái)講,C語(yǔ)言是一種遵循面向過(guò)程,或者俗稱(imperative programming/procedure programming)編程范式的程序設(shè)計(jì)語(yǔ)言,其關(guān)注點(diǎn)在于對(duì)解決問(wèn)題的Solver的抽象,所以C語(yǔ)言更接近于匯編。

而C++與其設(shè)計(jì)理念不同,C++試圖允許大家使用各種編程范式:

    比如首先支持使用C語(yǔ)言中的面向過(guò)程的編程范式。

    比如支持面向?qū)ο?/a>的編程范式,使得我們可以開(kāi)始抽象問(wèn)題本身,通過(guò)定義抽象數(shù)據(jù)類型(Abstract Data Type)來(lái)抽象問(wèn)題,封裝問(wèn)題對(duì)應(yīng)的數(shù)據(jù)在類作用域內(nèi)。然后我們可以合理地而不是濫用面向?qū)ο笤O(shè)計(jì)思想的封裝/繼承/ 多態(tài)來(lái)使得我們寫(xiě)的代碼更容易維護(hù)。

    比如模板的引入,使得我們可以進(jìn)行泛型編程;通過(guò)定義函數(shù)模板來(lái)利用函數(shù)參數(shù)列表的類型推導(dǎo)生成模板函數(shù),通過(guò)定義類模板結(jié)合比如偏特化的技術(shù)來(lái)生成模板類,函數(shù)模板和類模板都是編譯器看的東西,具體的代碼中,只有我們產(chǎn)生的具體模板函數(shù)和模板類會(huì)占代碼段的空間。當(dāng)然模板的引入還讓許多人可以進(jìn)行模板元編程,讓那些可以在編譯時(shí)候計(jì)算的東西放在編譯時(shí)進(jìn)行,有人還證明了模板元編程是圖靈完備的。

    比如lambda表達(dá)式的引入,讓我們進(jìn)行函數(shù)式編程更加自然,從語(yǔ)言層面而不是庫(kù)函數(shù)的層面來(lái)支持我們函數(shù)式編程。庫(kù)層面的話只能使用函數(shù)指針/或者C++的函數(shù)對(duì)象,來(lái)進(jìn)行函數(shù)式編程,并且函數(shù)一定會(huì)綁定有一個(gè)名字。有興趣的朋友可以學(xué)一學(xué)MIT Structure and Interpretation of Computer Programs。

    當(dāng)然還有其他的內(nèi)容,這里我僅僅根據(jù)我個(gè)人的理解,在這里拋磚引玉。感興趣的朋友可以搜集資料查看,如果有重要內(nèi)容補(bǔ)充的話,請(qǐng)進(jìn)行評(píng)論。

    (三/四) C/C++區(qū)別-重要的


    區(qū)別0,函數(shù)重載:C語(yǔ)言不支持 函數(shù)重載 ,其函數(shù)簽名不包含參數(shù)列表。

    因而在C語(yǔ)言中,也就不能夠像C++那樣 運(yùn)算符重載 了。下面通過(guò)兩個(gè)小代碼來(lái)說(shuō)明函數(shù)重載的區(qū)別,在c語(yǔ)言中我不得不取不同的函數(shù)名字來(lái)表達(dá)相近的操作加的意思。

    區(qū)別0,C代碼:non_overload.c

    #include "stdio.h" #include "stdlib.h" #include "string.h" int add_int(int lhs, int rhs) { return lhs + rhs; } void add_str(const char *lhs, int lhs_size, const char *rhs, int rhs_size, char *res, int *res_size) { memcpy(res, lhs, lhs_size); memcpy(res + lhs_size, rhs, rhs_size); res[lhs_size + rhs_size] = '\0'; *res_size = lhs_size + rhs_size; } int main() { printf("%d + %d = %d\n", 1, 2, add_int(1, 2)); const char *lhs = "abcd"; const char *rhs = "efgh"; char *res = malloc(sizeof(char) * 20); int res_size = 0; add_str(lhs, 4, rhs, 4, res, &res_size); printf("%s + %s = %s, size:%d\n", lhs, rhs, res, res_size); free(res); return 0; }

    區(qū)別0,C++代碼:with_overload.cpp

    #include #include using namespace std; int add(int lhs, int rhs) { return lhs + rhs; } void add(const char *lhs, int lhs_size, const char *rhs, int rhs_size, char *res, int &res_size) { memcpy(res, lhs, lhs_size); memcpy(res + lhs_size, rhs, rhs_size); res[lhs_size + rhs_size] = '\0'; res_size = lhs_size + rhs_size; } int main() { cout << "1 + 2 = " << add(1, 2) << endl; const char *lhs = "abcd"; const char *rhs = "efgh"; char *res = new char[20]; int res_size = 0; add(lhs, 4, rhs, 4, res, res_size); cout << lhs << " + " << rhs << " = " << res << ", size:" << res_size; delete[] res; return 0; }



    區(qū)別1,函數(shù)參數(shù)傳遞:在C語(yǔ)言中不從語(yǔ)言層面支持傳引用,需要通過(guò)指針類型的值傳遞實(shí)現(xiàn)傳引用的功能。

    詳細(xì)的Demo代碼如下,可以看到語(yǔ)法層面上的引用傳遞支持讓代碼看起來(lái)更舒服:

    區(qū)別1,C代碼:only_value_passing.c

    #include "stdio.h" void increment(int *int_ptr) { printf("Before: %d\n", *int_ptr); *int_ptr += 1; } int main() { int integer = 0; increment(&integer); printf("After: %d\n", integer); }

    區(qū)別1,C++代碼:ref_passing.cpp

    #include using namespace std; void increment(int &int_ptr) { cout << "Before: " << int_ptr << endl; int_ptr += 1; } int main() { int integer = 0; increment(integer); cout << "After: " << integer << endl; }



    區(qū)別2,函數(shù)的聲明(Declaration)和使用:在C語(yǔ)言中使用某個(gè)在其其他編譯單元的函數(shù)可以不先聲明(當(dāng)然這是一個(gè)不好的編碼習(xí)慣,在實(shí)際開(kāi)發(fā)中要極力避免),而在C++中這是不合法的。

    下面通過(guò)代碼來(lái)說(shuō)明這一點(diǎn),然后希望大家在使用的時(shí)候可以遵循好的編碼規(guī)范:

    區(qū)別2,C代碼:function_declaration.c

    #include int main() { foo(); return 0; } //雖然是一種不太好的編碼風(fēng)格 //一般好的C代碼都是要求在使用之前進(jìn)行函數(shù)聲明 //打開(kāi) -Wall 編譯器會(huì)警告我們不要這樣寫(xiě) void foo() { printf("Hello world"); }

    區(qū)別2,C++代碼:function_declaration.cpp

    #include using namespace std; void foo() { cout << "Hello world" << endl; } int main() { foo(); }



    區(qū)別3,模板:模板在我的理解中,是生成代碼的工具,但比C中的宏更安全更強(qiáng)大。

    比如函數(shù)模板是生成模板函數(shù)的工具,本身不會(huì)占有代碼段空間,而生成的模板函數(shù)則會(huì)占有代碼段空間,這一點(diǎn)是相當(dāng)有用的,我可不愿意讓我的代碼中有很多不會(huì)被調(diào)用的死代碼(dead codes)。C語(yǔ)言不支持 模板 ,因而不能輕松愉快地進(jìn)行所謂的泛型編程/模板元編程,只能用容易出錯(cuò),難以讀懂的宏定義預(yù)處理元編程

    在C++中模板主要有以下幾種:函數(shù)(function)模板,類(class)模板,變量(variable)模板(一種語(yǔ)法糖,讓我們不用繁瑣地寫(xiě)類模板),別名(alias)模板,具體可以參考 Cppcon2016 對(duì)應(yīng)的Youtube上發(fā)布的有關(guān) Template Normal Programming Part1 和 Template Normal Programming Part2 的視頻。這個(gè)作者用通俗易懂的例子給我們講述了模板在Modern Cpp中的使用,我個(gè)人強(qiáng)力推薦大家進(jìn)行觀看,不過(guò)大陸的朋友觀看Youtube需要翻墻。

    下面貼一個(gè)知乎的一個(gè)高贊同的回答鏈接 C++ 模板元編程的應(yīng)用有哪些,意義是什么 ,作者說(shuō)主要應(yīng)用有:編譯時(shí)計(jì)算,補(bǔ)充類型系統(tǒng),Domain Specific Language(是你說(shuō)的“開(kāi)發(fā)新語(yǔ)言”么?)。不過(guò)我個(gè)人不推薦大量地使用模板元編程,畢竟代碼是寫(xiě)給人看的,而且有其他語(yǔ)言,可以幫助我們用更好的方式做這些事情。我個(gè)人對(duì)語(yǔ)言又是沒(méi)有歧視的,哈哈。

    我下面用代碼展示下一個(gè)函數(shù)模板,說(shuō)明一下C++模板的泛型設(shè)計(jì),比C語(yǔ)言使用宏定義進(jìn)行預(yù)處理元編程要更可讀可寫(xiě),并且不太容易出錯(cuò)。

    區(qū)別3,C語(yǔ)言代碼:pre_processing.h

    ElemType FuncName(ElemType lhs, ElemType rhs) { return lhs + rhs; }

    區(qū)別3,C語(yǔ)言代碼:pre_processing.c

    #include "stdio.h" int main() { //自己做編譯器做的事情。。。 #undef ElemType #undef FuncName #define FuncName add_int #define ElemType int #include "pre_processing.h" printf("1 + 2 = %d\n", add_int(1, 2)); #undef ElemType #undef FuncName #define FuncName add_double #define ElemType double #include "pre_processing.h" printf("1 + 2 = %.2lf\n", add_double(1.0, 2)); }

    區(qū)別3,C++代碼,function_template.cpp

    #include using namespace std; template T add(T lhs, T rhs) { return lhs + rhs; } int main() { cout << "1 + 2 =" << add(1, 2) << endl; cout << "1.0 + 2.0 = " << add(1.0, static_cast(2)) << endl; cout << "1.0 + 2.0 = " << add(1.0, 2) << endl; }



    區(qū)別4,struct/class/enum/enum class, 在講述之前,我先提醒一下大家在C++中, struct 和 class 對(duì)編譯器看來(lái),只有默認(rèn)的成員變量作用域不同,一個(gè)是public,另一個(gè)是private,然后其他沒(méi)有區(qū)別;推薦一個(gè)知乎不錯(cuò)的回答 c++為什么要讓struct可以定義成員函數(shù)?,我引用一下里面的一些精華內(nèi)容,從3點(diǎn)概括C++編譯器可以做的“魔鬼操作”:

    1、對(duì)普通成員函數(shù),為它自動(dòng)添加this參數(shù),并在調(diào)用它時(shí),自動(dòng)把 obj.method() 轉(zhuǎn)換成 method(obj)格式;并識(shí)別出函數(shù)中涉及的、沒(méi)有顯式使用this的成員變量、為它加上this。除此之外,別的什么都不用做。2、對(duì)虛函數(shù),需要為繼承鏈上的每個(gè)類產(chǎn)生一個(gè)全局結(jié)構(gòu)體,在這個(gè)結(jié)構(gòu)體里按次序安排指向該類所有虛函數(shù)的指針,這就是虛函數(shù)表;然后在類里添加一個(gè)指向?qū)儆谧约旱奶摵瘮?shù)表的指針。那么,當(dāng)用戶調(diào)用某個(gè)對(duì)象的第N個(gè)虛函數(shù)時(shí),到虛函數(shù)表查找并獲取第N個(gè)函數(shù)指針指向的內(nèi)容;然后類似調(diào)用普通成員函數(shù)一樣,把 obj.method() 轉(zhuǎn)換成 method(obj)格式,多態(tài)就實(shí)現(xiàn)了。3、 當(dāng)然,除此之外,還要在編譯時(shí)執(zhí)行權(quán)限檢查,避免非法訪問(wèn)類的protect/private成員(struct默認(rèn)權(quán)限是public,class是private);以及另外一些瑣碎工作。

    好了,有了這些預(yù)備知識(shí)之后,我再提醒一下大家,在C語(yǔ)言的struct中,我們是不可以定義方法的,我個(gè)人對(duì)這個(gè)的看法是,C語(yǔ)言的設(shè)計(jì)者應(yīng)該認(rèn)為struct,union和指定內(nèi)存alignment的一些設(shè)計(jì)已經(jīng)足夠抽象出數(shù)據(jù)了,你要寫(xiě)一個(gè)solver就自己去寫(xiě)方法,如果你想要搞個(gè)函數(shù)對(duì)象也可以,把函數(shù)指針?lè)诺浇Y(jié)構(gòu)體里面去就好了。我覺(jué)得這很符合C遵循的面向過(guò)程(imperative programming/procedure programming)的編程范式,和從充斥面向?qū)ο蟮腸front發(fā)展而來(lái)的C++相比有很大不同。

    然后值得注意的是,在C語(yǔ)言中,我們使用strut和enum定義出來(lái)的類型的時(shí)候,我們要加上struct;如果C++中是 UserType var; ,那么在C中就是 struct UserType var;。然后C++11引入了 enum class 讓我們的枚舉類型更類型安全,詳細(xì)可以參考 C++11 FAQ中enum class的內(nèi)容 和 一個(gè)不錯(cuò)的講enum class的博客 ,我就不寫(xiě)代碼展示這些概念了,大家可以查看這兩個(gè)鏈接的內(nèi)容學(xué)習(xí)。



    區(qū)別5,面向?qū)ο?/strong>:C語(yǔ)言不支持面向?qū)ο?/strong>,不包含面向?qū)ο笾械膶?duì)象語(yǔ)義學(xué)。

    比如申請(qǐng)空間(可以是進(jìn)程棧空間或是通過(guò)malloc出來(lái)的進(jìn)程堆空間)后進(jìn)行構(gòu)造,然后在退出作用域的時(shí)候進(jìn)行析構(gòu)。因而,我們不能運(yùn)用所謂的 RAII(Resource Acquisition Is Initialization)機(jī)制 ,來(lái)管理資源(資源可以是堆空間,也可以是文件/網(wǎng)絡(luò)連接之類)。在C語(yǔ)言中,我們也不能使用封裝/繼承/和基于虛表(virtual-table)的多態(tài)等面向?qū)ο筇匦裕荒茏约捍鍯++編譯器,通過(guò)函數(shù)指針實(shí)現(xiàn)多態(tài)。

    下面我用一個(gè)給大家展示一下C++的RAII機(jī)制:
    區(qū)別5,Raii Demo C++代碼:raii_demo.cpp

    #include using namespace std; class ResourceManager { public: ResourceManager() { cout << "Acquire Resource, e.g, heap memory allocation/file handle " "operation/other resources" << endl; } ~ResourceManager() { cout << "Release Resource, e.g, heap memory allocation/file handle " "operation/other resources" << endl; } }; int main() { ResourceManager my_res_man; }

    運(yùn)行結(jié)果:

    Acquire Resource, e.g, heap memory allocation/file handle operation/other resources Release Resource, e.g, heap memory allocation/file handle operation/other resources

    下面我用簡(jiǎn)短的代碼展示一下C++面向?qū)ο蟮亩鄳B(tài),和C語(yǔ)言比較裸的函數(shù)指針(也就是面向?qū)ο蠖鄳B(tài)實(shí)現(xiàn)的基礎(chǔ))的多態(tài):
    區(qū)別5,多態(tài),C代碼:polymorphism_demo.c

    #include "stdio.h" #include "stdlib.h" struct Polygon { int width_, height_; void (*area_ptr_)(struct Polygon *ptr_ploy); }; struct Polygon *PolygonConstructor(int width, int height) { struct Polygon *ppoly = malloc(sizeof(struct Polygon)); ppoly->width_ = width, ppoly->height_ = height; return ppoly; } void area_rectangle(struct Polygon *ptr_ploy) { printf("Rectangle Width:%d,Height:%d\n", ptr_ploy->width_, ptr_ploy->height_); printf("Rectangle Area:%d\n", ptr_ploy->width_ * ptr_ploy->height_); } struct Polygon *RectangleConstructor(int width, int height) { struct Polygon *ppoly = PolygonConstructor(width, height); ppoly->area_ptr_ = area_rectangle; return ppoly; } void area_triangle(struct Polygon *ptr_ploy) { printf("Rectangle Width:%d,Height:%d\n", ptr_ploy->width_, ptr_ploy->height_); printf("Rectangle Area:%d\n", ptr_ploy->width_ * ptr_ploy->height_ / 2); } struct Polygon *TriangleConstructor(int width, int height) { struct Polygon *ppoly = PolygonConstructor(width, height); ppoly->area_ptr_ = area_triangle; return ppoly; } int main() { struct Polygon *ppoly_rec = RectangleConstructor(4, 5); ppoly_rec->area_ptr_(ppoly_rec); free(ppoly_rec); printf("\n"); struct Polygon *ppoly_tri = TriangleConstructor(4, 5); ppoly_tri->area_ptr_(ppoly_tri); free(ppoly_tri); }

    運(yùn)行結(jié)果:

    Rectangle Width:4,Height:5 Rectangle Area:20 Rectangle Width:4,Height:5 Rectangle Area:10

    區(qū)別5,多態(tài),C++代碼:polymorphism_demo.cpp

    //參考:http://www.cplusplus.com/doc/tutorial/polymorphism/ #include using namespace std; class Polygon { protected: int width_, height_; public: Polygon(int width, int height) : width_(width), height_(height) { cout << "Polygon Constructing..." << endl; } virtual ~Polygon() { cout << "Polygon Destructing..." << endl; } virtual void area() = 0; }; class Rectangle : public Polygon { public: Rectangle(int width, int height) : Polygon(width, height) { cout << "Rectangle Constructing..." << endl; } virtual ~Rectangle() { cout << "Rectangle Destructing..." << endl; }; void area() { cout << "Rectangle Width:" << width_ << ",Height" << height_ << endl; cout << "Rectangle Area:" << width_ * height_ << endl; } }; class Triangle : public Polygon { public: Triangle(int width, int height) : Polygon(width, height) {} virtual ~Triangle() { cout << "Triangle Destructing..." << endl; } void area() { cout << "Triangle Width:" << width_ << ",Height" << height_ << endl; cout << "Triangle Area:" << width_ * height_ / 2 << endl; } }; int main() { Polygon *ppoly_rec = new Rectangle(4, 5); ppoly_rec->area(); delete ppoly_rec; cout << endl << endl; Polygon *ppoly_tri = new Triangle(4, 5); ppoly_tri->area(); delete ppoly_tri; }

    運(yùn)行結(jié)果:

    Polygon Constructing... Rectangle Constructing... Rectangle Width:4,Height5 Rectangle Area:20 Rectangle Destructing... Polygon Destructing... Polygon Constructing... Triangle Width:4,Height5 Triangle Area:10 Triangle Destructing... Polygon Destructing...



    區(qū)別6,類型安全:C語(yǔ)言一些操作在C++中被認(rèn)為是非類型安全的,比如(void *)類型的數(shù)據(jù)使用。void * 是一種特殊的泛型指針(generic pointer),詳細(xì)可以看看 FAQ > Casting malloc 里面介紹的內(nèi)容。

    下面的這句代碼在C中可以編譯:

    int *x = malloc(sizeof(int) * 10);

    而在C++中則需要進(jìn)行強(qiáng)制類型轉(zhuǎn)換才可以:

    int *x = (int*) malloc(sizeof(int) * 10);



    區(qū)別7,內(nèi)存管理:C語(yǔ)言沒(méi)有 operator new/ operator delete 和 operator new[]/operator delete[],在全局作用域下也沒(méi)有用戶無(wú)法修改的函數(shù) placement new 和 placement delete;在C語(yǔ)言中只有 malloc 和 free 這兩個(gè)全局作用域的函數(shù)。

    區(qū)別7,我就不用代碼展示了,我給大家個(gè)鏈接,上面有代碼,C vs C++ - By Alex Allain 。



    區(qū)別8,Technical Specification提案(library上和language上):C++有更多的語(yǔ)言層面和庫(kù)層面的提案。比如在C++標(biāo)準(zhǔn)委員會(huì)搞的這個(gè) IsoCpp-Current Status 上,我們就可以看到C++新標(biāo)準(zhǔn)將考慮支持的一些庫(kù)層面和語(yǔ)言層面重要的提案,列舉幾個(gè)我比較關(guān)注的:Parallelism, Concurrency, Transactional Memory,F(xiàn)ile System, Networking,Concepts, Modules。這個(gè)內(nèi)容比較復(fù)雜,感興趣的朋友可以查閱鏈接看看。

    (四/四) C/C++區(qū)別-其他


    區(qū)別9,命名空間:C語(yǔ)言沒(méi)有namespace的說(shuō)法,所以很可能產(chǎn)生命名困難,因?yàn)樽兞慷际莋lobal的作用域。

    區(qū)別10,關(guān)鍵詞(語(yǔ)言特性):C++引入了一些關(guān)鍵字是C語(yǔ)言中沒(méi)有的,比如說(shuō)class, template, bool,C語(yǔ)言也有關(guān)鍵字是C++中沒(méi)有的,比如說(shuō)restrict。

    區(qū)別11,庫(kù):C++有更大的庫(kù),比如說(shuō)數(shù)學(xué)相關(guān)的庫(kù);比如說(shuō)C++11引入的多線程有關(guān)的庫(kù)#include;比如說(shuō)C++11中 #include 中定義的 std::move, std::forward, std::function 等有用的庫(kù)函數(shù);還有很多,我不一一列舉了。

    區(qū)別12,主函數(shù)返回值:C語(yǔ)言主函數(shù)必須加上 return 0; ,而C++的編譯器會(huì)自動(dòng)給主函數(shù)加上 return 0;。

    區(qū)別...,還有很多其他區(qū)別: 比如:異常機(jī)制I/O處理庫(kù)等等,在這里我就不一一列舉了。

    如有侵權(quán),請(qǐng)聯(lián)系刪除!