C++基础08-this指针-const修饰成员函数-函数返回引用/值
一、this指針
1、C++類對象中的成員變量和成員函數是分開存儲的。C語言中的內存四區模型仍然有效!
2、C++中類的普通成員函數都隱式包含一個指向當前對象的this指針。
3、靜態成員函數、成員變量屬于類
4、靜態成員函數與普通成員函數的區別
???????????? 靜態成員函數不包含指向具體對象的指針,靜態成員函數只能訪問靜態成員變量
???????????? 普通成員函數包含一個指向具體對象的指針
this指針不是const Test? *test;????????????????????? //this->a=100; 正確
this指針時一個常指針 是 Test * const test;? //this++; 錯誤
二、const修飾成員函數 到底修飾誰
??? //1 const 寫在哪個位置 沒有關系
void? OpVar(int a, int b)? const? 等價于 void?const OpVar(int a, int b)? 等價于 const? void? OpVar(int a, int b)?
?? ?//2 const 修飾的是誰
?? ?//2-1 const 修飾的形參a? 不是
?? ?//2-1 const 修飾的是屬性this->a this->b
?? ?//2-3 const 修飾的是this指針所指向的內存空間 修飾的是this指針
?? ?//void? OpVar(int a, int b) { //===>void OpVar(Test *const this,int a, int b)
?? ?void const OpVar(int a, int b)? const { //===>void OpVar(const Test * const this,int a, int b)? Test內存指向的空間不可更改
總結:類的成員函數尾部出現const修飾,修飾的是this指針,意思是不可更改this指針指向的內容
三、全局函數和成員函數的轉換
1、把全局函數轉化成成員函數,通過this指針隱藏左操作數
Test add(Test &t1, Test &t2)===》Test add(Test &t2)
2、把成員函數轉換成全局函數,多了一個參數
void printAB()===》void printAB(Test *pthis)
四、函數返回元素和引用
如果相對一個對象連續調用成員方法,每次都會改變對象本身,成員方法需要返回引用
Test& add(Test &t2) //*this //函數返回引?
{
????? this->a = this->a + t2.getA();
????? this->b = this->b + t2.getB();
????? return *this; //*操作讓this指針回到元素狀態
}
Test add2(Test &t2) //*this //函數返回元素
{
???? //t3是局部變量
???? Test t3(this->a+t2.getA(), this->b + t2.getB()) ;
???? return t3;
}
?this指針和const修飾函數應用舉例:
#if 1 #include<iostream> using namespace std; class Test { public:Test(int a, int b) { //--->Test(Test *this,int a,int b)this->a = a;this->b = b;}void printT() {cout << "a:" << a << endl;cout << "a:" << this->b << endl;}//1 const 寫的是什么位置 沒有關系//2 const 修飾的是誰//2-1 const 修飾的形參a 不是//2-1 const 修飾的是屬性this->a this->b//2-3 const 修飾的是this指針所指向的內存空間 修飾的是this指針//void OpVar(int a, int b) const { //===>void OpVar(Test *const this,int a, int b) const void const OpVar(int a, int b) const { //===>void OpVar(const Test * const this,int a, int b) const Test內存指向的空間不可更改a = 100;//this->a = 100; 報錯//this->b = 100;cout << a << endl;cout << b << endl;} protected: private:int a;int b; };void main() {Test t1(1, 2);t1.printT(); //printT(&t1)cout << "hello" << endl; } #endif全局函數和成員函數應用舉例:
#if 1 #include<iostream> using namespace std; class Test { public:int a;int b; public:Test(int a=0, int b=0) {this->a = a;this->b = b;}Test(int a) {this->a = a;this->b = 0;}Test TestAdd1(Test &t1) {/*Test t(0, 0);t.a = a + t1.b;t.b = b + t1.b;*/Test t(this->a + t1.a, this->b + t1.b);return t;}void TestAdd2(Test &t1) {this->a = a + t1.a;this->b = b + t1.b;}//返回一個引用 相當于返回變量自身Test& TestAdd3(Test &t1) {this->a = this->a + t1.a;this->b = this->b + t1.b;return *this; //相當于把*(&t1)又回到了t1元素}void printT() {cout << "a:" << a << endl;cout << "b:" << b<<endl;}~Test() {cout << "析構函數" << endl;}}; //把成員函數轉換成 全局函數 多一個參數 void printT(Test *pT) {cout << "a:" << pT->a << endl;cout << "b:" << pT->b << endl; } //全局函數 轉成 成員函數 少一個參數 Test TestAdd(Test &t1, Test &t2) {Test t(0,0);t.a = t1.a + t2.a;t.b = t1.b + t2.b;return t; } void test01() {Test t1(1, 2);Test t2(3, 4);Test t3;//全局函數方法t3= TestAdd(t1, t2); } /* 析構函數 析構函數 析構函數 析構函數 析構函數 */ void test02() {Test t1(1, 2);Test t2(3, 4);{//成員函數方法Test t3 = t1.TestAdd1(t2); //匿名對象直接轉化為t3t3.printT();Test t4;t4 = t1.TestAdd1(t2); //匿名對象復制給t4t4.printT();}t1.TestAdd2(t2); } /* 析構函數 a:4 b:6 析構函數 析構函數 a:4 b:6 析構函數 析構函數 析構函數 析構函數 */ void test03() {Test t1(1, 2);Test t2(3, 4);//t2=t2+t1;t2.TestAdd2(t1);t2.printT();t2.TestAdd3(t1);t2.printT(); } /* a:4 b:6 a:5 b:8 析構函數 析構函數 */ int main() {//test01();//test02();test03();return 0; } #endif函數返回引用和元素示例:
#if 1 #include<iostream> using namespace std; class Test { public:int a;int b; public:Test(int a = 0, int b = 0) {this->a = a;this->b = b;}Test(int a) {this->a = a;this->b = 0;}Test TestAdd1(Test &t1) {Test t(this->a + t1.a, this->b + t1.b);return t;}void TestAdd2(Test &t1) {this->a = a + t1.a;this->b = b + t1.b;}Test TestAdd3(Test &t1) {this->a = this->a + t1.a;this->b = this->b + t1.b;return *this; //如果想返回一個對象的本身,在成員方法中 使用*this返回}//返回一個引用 相當于返回變量自身Test& TestAdd4(Test &t1) {this->a = this->a + t1.a;this->b = this->b + t1.b;return *this; //相當于把*(&t1)又回到了t1元素}void printT() {cout << "a:" << a << " b:" << b << endl;}~Test() {cout << "析構函數" << endl;}}; //Test TestAdd1(Test &t1) //void TestAdd2(Test &t1) //Test TestAdd3(Test &t1) //Test& TestAdd4(Test &t1) void test01() {Test t1(10, 20);Test t2(100, 200);Test t3 = t1.TestAdd1(t2);t3.printT(); } /* 析構函數 a:110 b:220 析構函數 析構函數 析構函數 */ void test02() {Test t1(10, 20);Test t2(100, 200);t1.TestAdd2(t2);t1.printT(); } /* a:110 b:220 析構函數 析構函數 */ void test03() {Test t1(10, 20);Test t2(100, 200);//((t1 += t2) += t2 )+= t2;//t1.TestAdd2(t2).TestAdd2(t2); 報錯 無法連加 t1.TestAdd2(t2)返回值為空t1.TestAdd3(t2).TestAdd3(t2); //a:110 b:220 //t1.TestAdd3(t2)返回為匿名對象//所以在第二次.TestAdd3(t2)時是匿名對象的相加,并不是t1t1.printT(); } /* 析構函數 析構函數 a:110 b:220 析構函數 析構函數 */ void test04() {Test t1(10, 20);Test t2(100, 200);//如果相對一個對象連續調用成員方法,每次都會改變對象本身,成員方法需要返回引用t1.TestAdd4(t2).TestAdd4(t2); //a:210 b:420 //t1.TestAdd4(t2)返回的是計算后的對象本身//所以在第二次.TestAdd4(t2)時是計算后的對象相加t1.printT(); } /* a:210 b:420 析構函數 析構函數 */ int main() {cout << "-----------test01-----------" << endl;test01();cout << "-----------test02-----------" << endl;test02();cout << "-----------test03-----------" << endl;test03();cout << "-----------test04-----------" << endl;test04();return 0; } #endif?
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的C++基础08-this指针-const修饰成员函数-函数返回引用/值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: codesys raspberry pi
- 下一篇: LNK2019无法解析的外部符号 pub