日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

c语言指针++_C ++此指针| 查找输出程序| 套装3

發(fā)布時(shí)間:2023/12/1 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言指针++_C ++此指针| 查找输出程序| 套装3 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

c語言指針++

Program 1:

程序1:

#include <iostream> using namespace std;class Test {int VAL;public:Test(int v){VAL = v;}Test* Sum(Test T1, Test T2){VAL = T1.VAL + T2.VAL;return this;}void print(){cout << VAL << " ";} };int main() {Test T1(10);Test T2(20);Test* T3;T3 = T1.Sum(T1, T2);T1.print();T2.print();T3->print();return 0; }

Output:

輸出:

30 20 30

Explanation:

說明:

Consider the sum() function, the function is taking two objects of Test class arguments and returning the pointer of the current object using this.?

考慮sum()函數(shù),該函數(shù)接收Test類參數(shù)的兩個(gè)對象,并使用this返回當(dāng)前對象的指針。

And, in the main() function, we created two objects T1, T2, and a pointer T3, which is holding the current object pointer returned by sum(). The sum() is adding the values of T1 and T2 and assigning in T1 because we are calling the function sum() using T1 and returning the address of T1 which is assigning to the pointer T3.

并且,在main()函數(shù)中,我們創(chuàng)建了兩個(gè)對象T1 , T2和一個(gè)指針T3 ,該指針保存了sum()返回的當(dāng)前對象指針。 sum()將T1和T2的值相加并在T1中賦值,因?yàn)槲覀冋谑褂肨1調(diào)用函數(shù)sum()并返回分配給指針T3的T1地址。

Program 2:

程式2:

#include <iostream> using namespace std;class Test { public:Test call1(){cout << "call1 ";return *this;}Test call2(){cout << "call2 ";return *this;}Test call3(){cout << "call3 ";return *this;} };int main() {Test T1;T1.call1().call2().call3();return 0; }

Output:

輸出:

call1 call2 call3

Explanation:

說明:

Here, we implemented a cascaded function call using this pointer, and created the class Test with 3 member functions call1(), call2(), and call3(). All these functions will return the current object of the class using *this.

在這里,我們使用此指針實(shí)現(xiàn)了級聯(lián)函數(shù)調(diào)用,并使用3個(gè)成員函數(shù) call1() , call2()和call3()創(chuàng)建了Test類。 所有這些函數(shù)都將使用* this返回類的當(dāng)前對象。

翻譯自: https://www.includehelp.com/cpp-tutorial/this-pointer-find-output-programs-set-3.aspx

c語言指針++

總結(jié)

以上是生活随笔為你收集整理的c语言指针++_C ++此指针| 查找输出程序| 套装3的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。