C C++ 是干什么的
成都創(chuàng)新互聯(lián)公司是一家專(zhuān)注于網(wǎng)站制作、成都網(wǎng)站建設(shè)與策劃設(shè)計(jì),福州網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)公司做網(wǎng)站,專(zhuān)注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專(zhuān)業(yè)建站公司;建站業(yè)務(wù)涵蓋:福州等地區(qū)。福州做網(wǎng)站價(jià)格咨詢(xún):18980820575
1簡(jiǎn)單的C++ Hello World!
#include "iostream" using namespace std; int main() { cout << "Hello World!\n" ;//<< endl; cout << "Hello World!" << endl; return 0; } 編譯運(yùn)行: chunli@Linux:~/c++$ g++ main.cpp && ./a.out Hello World! Hello World! chunli@Linux:~/c++$
2面向?qū)ο蟮恼J(rèn)識(shí):
求圓的面積,過(guò)程方法
#include "iostream" using namespace std; int main() { double r = 0; double s = 0; cout << "請(qǐng)輸入圓的半徑:" ;//<< endl; cin >> r; s = 3.1415926 * r * r; cout <<"圓的面積是:"<< s << endl; return 0; } 編譯: chunli@Linux:~/c++$ g++ main.cpp && ./a.out 請(qǐng)輸入圓的半徑:10 圓的面積是:314.159
求圓的面積,面向?qū)ο蠓椒?/p>
#include "iostream" using namespace std; //類(lèi)是一個(gè)數(shù)據(jù)類(lèi)型,(固定大小內(nèi)存塊的別名); //定義一個(gè)類(lèi),是一個(gè)抽象的概念,不會(huì)給你分配內(nèi)存 //用數(shù)據(jù)類(lèi)型定義變量的時(shí)候,才會(huì)分配內(nèi)存 class MyCicle { public : double m_s; double m_r; void set_R(double r) { m_r = r; } double gets() { return 3.1415926 * m_r * m_r; } }; int main() { MyCicle my1; double r; cout << "請(qǐng)輸入圓的半徑:" ; cin >> r; my1.set_R(r); cout <<"圓的面積是:"<< my1.gets() << endl; return 0; } 編譯運(yùn)行: chunli@Linux:~/c++$ g++ main.cpp && ./a.out 請(qǐng)輸入圓的半徑:11 圓的面積是:380.133
【經(jīng)典】初學(xué)者易犯錯(cuò)誤:
#include "iostream" using namespace std; class MyCicle { public : double r; double s = 3.14 * r *r; }; int main() { MyCicle my1; cout << "請(qǐng)輸入圓的半徑:" ; cin >> my1.r; cout <<"圓的面積是:"<< my1.s << endl; return 0; } 編譯運(yùn)行:Lchunli@Linux:~/c++$ g++ -std=c++11 main.cpp && ./a.out 請(qǐng)輸入圓的半徑:10 圓的面積是:0
namespace 1,標(biāo)準(zhǔn)用法:
因?yàn)閕ostream中沒(méi)有引入標(biāo)準(zhǔn)std,需要程序員手工寫(xiě)
chunli@Linux:~/c++$ cat main.cpp #include "iostream" using namespace std; int main() { cout << "Hello World!"<<endl ; return 0; } 編譯運(yùn)行: chunli@Linux:~/c++$ g++ main.cpp && ./a.out Hello World!
namespace 2,實(shí)際用法:
如果不寫(xiě) using namespace std ,那么需要顯式的引入std
chunli@Linux:~/c++$ cat main.cpp #include "iostream" //using namespace std; int main() { std::cout << "Hello World!"<<std::endl ; return 0; } chunli@Linux:~/c++$ g++ main.cpp && ./a.out Hello World!
自定義namespace
chunli@Linux:~/c++$ cat main.cpp #include "iostream" using namespace std; namespace namespaceA { int a ; } namespace namaspaceB { int a ; namespace namaspaceC { int a ; } } int main() { using namespace namespaceA; a = 10; cout << a <<endl ; return 0; } 編譯運(yùn)行: chunli@Linux:~/c++$ g++ main.cpp && ./a.out 10
兩個(gè)namespace中含有同樣的變量,編譯不通過(guò)
chunli@Linux:~/c++$ cat main.cpp #include "iostream" using namespace std; namespace namespaceA { int a ; } namespace namespaceB { int a ; namespace namespaceC { int a ; } } int main() { using namespace namespaceA; using namespace namespaceB; a = 10; cout << a <<endl ; return 0; } chunli@Linux:~/c++$ g++ main.cpp && ./a.out main.cpp: In function ‘int main()’: main.cpp:24:2: error: reference to ‘a(chǎn)’ is ambiguous a = 10; ^ 可以看到不明確a是哪一個(gè)
顯式的引用不同namespace中的值
chunli@Linux:~/c++$ cat main.cpp #include "iostream" using namespace std; namespace namespaceA { int a ; } namespace namespaceB { int a ; namespace namespaceC { int a ; } } int main() { using namespace namespaceA; using namespace namespaceB; namespaceA::a = 10; namespaceB::a = 20; cout << namespaceA::a <<endl ; cout << namespaceB::a <<endl ; return 0; } chunli@Linux:~/c++$ g++ main.cpp && ./a.out 10 20
使用嵌套的namespace
#include "iostream" using namespace std; namespace namespaceB { int a ; namespace namespaceC { struct Teacher { int age; }; } } int main() { using namespace namespaceB::namespaceC; Teacher t1; t1.age =10; cout << t1.age<<endl ; return 0; } 編譯: chunli@Linux:~/c++$ g++ main.cpp && ./a.out 10
=============================================================================
C++與C的區(qū)別:
1
2 register類(lèi)型增強(qiáng)
chunli@Linux:~/c++$ cat 123.c #include <stdio.h> int main() { register int a = 0; printf("%p \n",&a); return 0; } C語(yǔ)言編譯不通過(guò): chunli@Linux:~/c++$ gcc 123.c && ./a.out 123.c: In function ‘main’: 123.c:5:2: error: address of register variable ‘a(chǎn)’ requested printf("%p \n",&a); ^ C++編譯通過(guò): chunli@Linux:~/c++$ g++ 123.c && ./a.out 0x7ffe7ac2cc3c
C語(yǔ)言缺陷,C++修復(fù)
chunli@Linux:~/c++$ cat 123.c #include <stdio.h> f(i) { printf("i = %d \n",i); } g() { return 5; } int main() { f(10); printf("%d \n",g()); printf("%d \n",g(1,2,3,4)); return 0; } C語(yǔ)言編譯OK chunli@Linux:~/c++$ gcc 123.c && ./a.out i = 10 5 5 C++編譯報(bào)錯(cuò): chunli@Linux:~/c++$ g++ 123.c && ./a.out 123.c:2:2: error: expected constructor, destructor, or type conversion before ‘(’ token f(i) ^ 123.c:6:3: error: ISO C++ forbids declaration of ‘g’ with no type [-fpermissive] g() ^
C++對(duì)C的加強(qiáng):
1 namespace命名空間
1 C++命名空間基本常識(shí)
所謂namespace,是指標(biāo)識(shí)符的各種可見(jiàn)范圍。C++標(biāo)準(zhǔn)程序庫(kù)中的所有標(biāo)識(shí)符都被定義于一個(gè)名為std的namespace中。
一 :<iostream>和<iostream.h>格式不一樣,前者沒(méi)有后綴,實(shí)際上,在你的編譯器include文件夾里面可以看到,二者是兩個(gè)文件,打開(kāi)文件就會(huì)發(fā)現(xiàn),里面的代碼是不一樣的。后綴為.h的頭文件c++標(biāo)準(zhǔn)已經(jīng)明確提出不支持了,早些的實(shí)現(xiàn)將標(biāo)準(zhǔn)庫(kù)功能定義在全局空間里,聲明在帶.h后綴的頭文件里,c++標(biāo)準(zhǔn)為了和C區(qū)別開(kāi),也為了正確使用命名空間,規(guī)定頭文件不使用后綴.h。 因此,
1)當(dāng)使用<iostream.h>時(shí),相當(dāng)于在c中調(diào)用庫(kù)函數(shù),使用的是全局命名空間,也就是早期的c++實(shí)現(xiàn);
2)當(dāng)使用<iostream>的時(shí)候,該頭文件沒(méi)有定義全局命名空間,必須使用namespace std;這樣才能正確使用cout。
二: 由于namespace的概念,使用C++標(biāo)準(zhǔn)程序庫(kù)的任何標(biāo)識(shí)符時(shí),可以有三種選擇:
1、直接指定標(biāo)識(shí)符。例如std::ostream而不是ostream。完整語(yǔ)句如下: std::cout << std::hex << 3.4 << std::endl;
2、使用using關(guān)鍵字。 using std::cout; using std::endl; using std::cin; 以上程序可以寫(xiě)成 cout << std::hex << 3.4 << endl;
3、最方便的就是使用using namespace std; 例如: using namespace std;這樣命名空間std內(nèi)定義的所有標(biāo)識(shí)符都有效(曝光)。就好像它們被聲明為全局變量一樣。那么以上語(yǔ)句可以如下寫(xiě): cout <<hex << 3.4 << endl;因?yàn)闃?biāo)準(zhǔn)庫(kù)非常的龐大,所以程序員在選擇的類(lèi)的名稱(chēng)或函數(shù)名 時(shí)就很有可能和標(biāo)準(zhǔn)庫(kù)中的某個(gè)名字相同。所以為了避免這種情況所造成的名字沖突,就把標(biāo)準(zhǔn)庫(kù)中的一切都被放在名字空間std中。但這又會(huì)帶來(lái)了一個(gè)新問(wèn) 題。無(wú)數(shù)原有的C++代碼都依賴(lài)于使用了多年的偽標(biāo)準(zhǔn)庫(kù)中的功能,他們都是在全局空間下的。所以就有了<iostream.h> 和<iostream>等等這樣的頭文件,一個(gè)是為了兼容以前的C++代碼,一個(gè)是為了支持新的標(biāo)準(zhǔn)。命名空間std封裝的是標(biāo)準(zhǔn)程序庫(kù)的名稱(chēng),標(biāo)準(zhǔn)程序庫(kù)為了和以前的頭文件區(qū)別,一般不加".h"
4.2 “實(shí)用性”增加
#include "iostream" using namespace std; //C語(yǔ)言中的變量都必須在作用域開(kāi)始的位置定義??! //C++中更強(qiáng)調(diào)語(yǔ)言的“實(shí)用性”,所有的變量都可以在需要使用時(shí)再定義。 int main11() { int i = 0; printf("ddd"); int k; system("pause"); return 0; }
4.3 register關(guān)鍵字增強(qiáng)
//register關(guān)鍵字 請(qǐng)求編譯器讓變量a直接放在寄存器里面,速度快 //在c語(yǔ)言中 register修飾的變量 不能取地址,但是在c++里面做了內(nèi)容 /* //1 register關(guān)鍵字的變化 register關(guān)鍵字請(qǐng)求“編譯器”將局部變量存儲(chǔ)于寄存器中 C語(yǔ)言中無(wú)法取得register變量地址 在C++中依然支持register關(guān)鍵字 C++編譯器有自己的優(yōu)化方式,不使用register也可能做優(yōu)化 C++中可以取得register變量的地址 //2 C++編譯器發(fā)現(xiàn)程序中需要取register變量的地址時(shí),register對(duì)變量的聲明變得無(wú)效。 //3 早期C語(yǔ)言編譯器不會(huì)對(duì)代碼進(jìn)行優(yōu)化,因此register變量是一個(gè)很好的補(bǔ)充。 */ int main22() { register int a = 0; printf("&a = %x\n", &a); system("pause"); return 0; }
4.4變量檢測(cè)增強(qiáng)
/* 在C語(yǔ)言中,重復(fù)定義多個(gè)同名的全局變量是合法的 在C++中,不允許定義多個(gè)同名的全局變量 C語(yǔ)言中多個(gè)同名的全局變量最終會(huì)被鏈接到全局?jǐn)?shù)據(jù)區(qū)的同一個(gè)地址空間上 int g_var; int g_var = 1; C++直接拒絕這種二義性的做法。 */ int main(int argc, char *argv[]) { printf("g_var = %d\n", g_var); return 0; }
4.5 struct類(lèi)型加強(qiáng)
struct類(lèi)型的加強(qiáng):
C語(yǔ)言的struct定義了一組變量的集合,C編譯器并不認(rèn)為這是一種新的類(lèi)型
C++中的struct是一個(gè)新類(lèi)型的定義聲明
struct Student { char name[100]; int age; }; int main(int argc, char *argv[]) { Student s1 = {"wang", 1}; Student s2 = {"wang2", 2}; return 0; }
4.6 C++中所有的變量和函數(shù)都必須有類(lèi)型
/* C++中所有的變量和函數(shù)都必須有類(lèi)型 C語(yǔ)言中的默認(rèn)類(lèi)型在C++中是不合法的 函數(shù)f的返回值是什么類(lèi)型,參數(shù)又是什么類(lèi)型? 函數(shù)g可以接受多少個(gè)參數(shù)? */ //更換成.cpp試試 f(i) { printf("i = %d\n", i); } g() { return 5; } int main(int argc, char *argv[]) { f(10); printf("g() = %d\n", g(1, 2, 3, 4, 5)); getchar(); return 0; }
【總結(jié)】:
/*
在C語(yǔ)言中
int f( );表示返回值為int,接受任意參數(shù)的函數(shù)
int f(void);表示返回值為int的無(wú)參函數(shù)
在C++中
int f( );和int f(void)具有相同的意義,都表示返回值為int的無(wú)參函數(shù)
*/
C++更加強(qiáng)調(diào)類(lèi)型,任意的程序元素都必須顯示指明類(lèi)型
4.2-4.6屬于語(yǔ)法級(jí)別的增強(qiáng)。
4.7新增Bool類(lèi)型關(guān)鍵字
/* C++中的布爾類(lèi)型 C++在C語(yǔ)言的基本類(lèi)型系統(tǒng)之上增加了bool C++中的bool可取的值只有true和false 理論上bool只占用一個(gè)字節(jié), 如果多個(gè)bool變量定義在一起,可能會(huì)各占一個(gè)bit,這取決于編譯器的實(shí)現(xiàn) true代表真值,編譯器內(nèi)部用1來(lái)表示 false代表非真值,編譯器內(nèi)部用0來(lái)表示 bool類(lèi)型只有true(非0)和false(0)兩個(gè)值 C++編譯器會(huì)在賦值時(shí)將非0值轉(zhuǎn)換為true,0值轉(zhuǎn)換為false */ int main(int argc, char *argv[]) { int a; bool b = true; printf("b = %d, sizeof(b) = %d\n", b, sizeof(b)); b = 4; a = b; printf("a = %d, b = %d\n", a, b); b = -4; a = b; printf("a = %d, b = %d\n", a, b); a = 10; b = a; printf("a = %d, b = %d\n", a, b); b = 0; printf("b = %d\n", b); system("pause"); return 0; }
4.8三目運(yùn)算符功能增強(qiáng)
1三目運(yùn)算符在C和C++編譯器的表現(xiàn)
int main() { int a = 10; int b = 20; //返回一個(gè)最小數(shù) 并且給最小數(shù)賦值成3 //三目運(yùn)算符是一個(gè)表達(dá)式 ,表達(dá)式不可能做左值 (a < b ? a : b )= 30; printf("a = %d, b = %d\n", a, b); system("pause"); return 0; }
2結(jié)論
1)C語(yǔ)言返回變量的值 C++語(yǔ)言是返回變量本身
C語(yǔ)言中的三目運(yùn)算符返回的是變量值,不能作為左值使用
C++中的三目運(yùn)算符可直接返回變量本身,因此可以出現(xiàn)在程序的任何地方
2)注意:三目運(yùn)算符可能返回的值中如果有一個(gè)是常量值,則不能作為左值使用
(a < b ? 1 : b )= 30;
3)C語(yǔ)言如何支持類(lèi)似C++的特性呢?
====>當(dāng)左值的條件:要有內(nèi)存空間;C++編譯器幫助程序員取了一個(gè)地址而已
思考:如何讓C中的三目運(yùn)算法當(dāng)左值呢?
C++ bool類(lèi)型
只要是非0數(shù)字賦予bool類(lèi)型,全部為true
chunli@Linux:~/c++$ cat main.cpp #include "iostream" using namespace std; int main() { bool b1 = true; cout << sizeof(b1) << endl; b1 = 10; cout << b1 << endl; b1 = -10; cout << b1 << endl; b1 = 0; cout << b1 << endl; return 0; } chunli@Linux:~/c++$ g++ main.cpp && ./a.out 1 1 1 0 chunli@Linux:~/c++$
C++ 對(duì)三目運(yùn)算符的增強(qiáng):
chunli@Linux:~/c++$ cat c.c #include<stdio.h> int main() { int a = 10; int b = 20; //在C語(yǔ)言中表達(dá)式返回值,返回變量的本身 //在++語(yǔ)言中返回變量本身 (a < b ? a: b) = 30; printf("%d \n",a); printf("%d \n",b); return 0; } chunli@Linux:~/c++$ gcc c.c && ./a.out c.c: In function ‘main’: c.c:6:17: error: lvalue required as left operand of assignment (a < b ? a: b) = 30; ^ chunli@Linux:~/c++$ g++ c.c && ./a.out 30 20
在C語(yǔ)言中如何實(shí)現(xiàn)在C++中編譯器的效果
C++編譯器幫程序員完成了剛才我們的動(dòng)作
chunli@Linux:~/c++$ cat c.c #include<stdio.h> int main() { int a = 10; int b = 20; *(a < b ? &a: &b) = 30; printf("%d \n",a); printf("%d \n",b); return 0; } chunli@Linux:~/c++$ gcc c.c && ./a.out 30 20 chunli@Linux:~/c++$ g++ c.c && ./a.out 30 20
C++ const 基礎(chǔ)
#include "iostream" using namespace std; int main() { const int a = 10; int const b =11; //與上面的一樣 const int *c ; //指針指向的內(nèi)存區(qū)域內(nèi)的數(shù)值不能修改 int * const d; //指針不能亂指 const int * const e; //只讀指針 return 0; }
const C C++語(yǔ)言中的冒牌貨
1,C語(yǔ)言中的const真是個(gè)冒牌貨
chunli@Linux:~/c++$ cat c.c #include<stdio.h> int main() { const int a = 10; int *p = (int *)&a; *p = 20; printf("%d \n",a); return 0; } chunli@Linux:~/c++$ gcc c.c && ./a.out 20
2,C++語(yǔ)言中的const真是真貨
chunli@Linux:~/c++$ cat main.cpp #include "iostream" using namespace std; int main() { const int a = 10; int *p = NULL; p = (int *)&a; *p = 20; cout << a <<endl; cout << *p <<endl; return 0; } chunli@Linux:~/c++$ g++ -Wall main.cpp && ./a.out 10 20
const 與define
GCC支持這種寫(xiě)法,微軟的不支持
chunli@Linux:~/c++$ cat main.cpp #include "iostream" using namespace std; int main() { int a =10; int b =30; int arr[a+b]; cout << sizeof(arr)/sizeof(int)<<endl; return 0; } chunli@Linux:~/c++$ g++ -Wall main.cpp && ./a.out 40
C++編譯器cosnt與宏定義 相似之處
chunli@Linux:~/c++$ cat main.cpp #include "iostream" using namespace std; #define a 20 int main() { //int a =10; int b =30; int arr[a+b]; cout << sizeof(arr)/sizeof(int)<<endl; return 0; } chunli@Linux:~/c++$ g++ -Wall main.cpp && ./a.out 50
宏定義作用范圍
chunli@Linux:~/c++$ cat main.cpp #include "iostream" using namespace std; void fun1() { #define a 20 cout << "Hello World!" << endl; } void fun2() { cout << a << endl; } int main() { fun1(); fun2(); return 0; } chunli@Linux:~/c++$ g++ -Wall main.cpp && ./a.out Hello World! 20
取消宏定義
chunli@Linux:~/c++$ cat main.cpp #include "iostream" using namespace std; void fun1() { #define a 20 cout << "Hello World!" << endl; #undef a //取消宏定義a //#undef //取消所有宏定義 } void fun2() { cout << a << endl; } int main() { fun1(); fun2(); return 0; } chunli@Linux:~/c++$ g++ -Wall main.cpp && ./a.out main.cpp: In function ‘void fun2()’: main.cpp:13:10: error: ‘a(chǎn)’ was not declared in this scope cout << a << endl; ^
【引用】
1,基礎(chǔ)知識(shí),兩個(gè)變量的地址是一樣的
chunli@Linux:~/c++$ cat main.cpp #include "iostream" using namespace std; int main() { int a = 10; int &b = a; b = 20; cout<< a << endl; cout<< &a << endl; cout<< &b << endl; return 0; } chunli@Linux:~/c++$ g++ -Wall main.cpp && ./a.out 20 0x7ffec647cc94 0x7ffec647cc94 這就是傳說(shuō)中的引用,不能用C語(yǔ)言去思考
1,定義一個(gè)引用,而沒(méi)有引用值,編譯報(bào)錯(cuò);
chunli@Linux:~/c++$ cat main.cpp #include "iostream" using namespace std; int main() { int a = 10; cout << a << endl; int &b ; return 0; } chunli@Linux:~/c++$ g++ -Wall main.cpp && ./a.out main.cpp: In function ‘int main()’: main.cpp:7:7: error: ‘b’ declared as reference but not initialized int &b ; ^
引用的實(shí)際應(yīng)用,當(dāng)函數(shù)參數(shù)
引用當(dāng)函數(shù)參數(shù)不需要初始化
chunli@Linux:~/c++$ cat main.cpp #include "iostream" using namespace std; void fun1(int &a,int &b) { a = a ^ b; b = a ^ b; a = a ^ b; } void fun2(int *a,int *b) { *a = *a ^ *b; *b = *a ^ *b; *a = *a ^ *b; } int main() { int var1 = 10; int var2 = 20; cout << var1 << " "<<var2<< endl; fun1(var1,var2); cout << var1 << " "<<var2<< endl; fun2(&var1,&var2); cout << var1 << " "<<var2<< endl; return 0; } chunli@Linux:~/c++$ g++ -Wall main.cpp && ./a.out 10 20 20 10 10 20 chunli@Linux:~/c++$
復(fù)雜數(shù)據(jù)類(lèi)型與引用;
chunli@Linux:~/c++$ cat main.cpp #include "iostream" using namespace std; struct Teacher { char name[64]; int age; }; void fun1(Teacher *p) { cout << p->age << endl; p->age = 11; } void fun2(Teacher &p) { cout << p.age << endl; p.age = 22; } void fun3(Teacher p) { cout << p.age << endl; p.age = 33; } int main() { Teacher t1; t1.age = 20; fun1(&t1); cout << "1 " << t1.age << endl; fun2(t1); cout << "2 " << t1.age << endl; fun3(t1); cout << "3 " << t1.age << endl; return 0; } chunli@Linux:~/c++$ g++ -Wall main.cpp && ./a.out 20 1 11 11 2 22 22 3 22 chunli@Linux:~/c++$
結(jié)構(gòu)體的引用類(lèi)型實(shí)質(zhì)
可以看出與指針很像
chunli@Linux:~/c++$ cat main.cpp #include "iostream" using namespace std; struct Teacher { char &c; }; int main() { cout << sizeof(struct Teacher)<< endl; return 0; } chunli@Linux:~/c++$ g++ -Wall main.cpp && ./a.out 8
引用的本質(zhì),看圖
chunli@Linux:~/c++$ cat main.cpp #include "iostream" using namespace std; void fun1(int &a) { a = 10; } void fun2(int *a) { *a = 20; } int main() { int var = 0; fun1(var); cout << var<< endl; fun2(&var); cout << var<< endl; return 0; } chunli@Linux:~/c++$ g++ -Wall main.cpp && ./a.out 10 20
引用的難點(diǎn):
返回引用數(shù)據(jù)類(lèi)型的函數(shù),用什么數(shù)據(jù)類(lèi)型來(lái)接?
1,用普通變量來(lái)接,相當(dāng)于數(shù)值的傳遞
2,用引用類(lèi)型數(shù)據(jù)來(lái)接,相當(dāng)于地址的傳遞(危險(xiǎn)),返回的地址是已經(jīng)被析構(gòu)的
GCC會(huì)有警告
VS不會(huì)有警告
chunli@Linux:~/c++$ cat main.cpp #include "iostream" using namespace std; int& fun1() { int a = 10; return a; } int main() { int a = fun1(); cout << a<< endl; int &b = fun1(); cout << b<< endl; return 0; } chunli@Linux:~/c++$ g++ main.cpp && ./a.out main.cpp: In function ‘int& fun1()’: main.cpp:5:6: warning: reference to local variable ‘a(chǎn)’ returned [-Wreturn-local-addr] int a = 10; ^ 10 10
函數(shù)返回應(yīng)用類(lèi)型,變量是全局生命周期
chunli@Linux:~/c++$ cat main.cpp #include "iostream" using namespace std; int a = 20; int& fun1() { return a; } int main() { int a = fun1(); cout << a<< endl; int &b = fun1(); cout << b<< endl; return 0; } chunli@Linux:~/c++$ g++ main.cpp && ./a.out 20 20
函數(shù)當(dāng)左值,注意變量的生命周期
chunli@Linux:~/c++$ cat main.cpp #include "iostream" using namespace std; int a = 20; int& fun1() { cout << "in fun1 " <<a << endl; return a; } int main() { //當(dāng)左值 fun1() = 100; cout << "in fun2 " << a<< endl; //當(dāng)右值 int a = fun1(); return 0; } chunli@Linux:~/c++$ g++ main.cpp && ./a.out in fun1 20 in fun2 100 in fun1 100
文章名稱(chēng):C++基礎(chǔ)1C與C++的功能擴(kuò)展語(yǔ)法增強(qiáng) namesapceconst引用
瀏覽路徑:http://chinadenli.net/article44/pgphhe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、移動(dòng)網(wǎng)站建設(shè)、品牌網(wǎng)站制作、ChatGPT、關(guān)鍵詞優(yōu)化、定制開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)