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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

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

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

c語言指針++

Program 1:

程序1:

#include <iostream> using namespace std;int main() {int A = 10;this* ptr;ptr = &A;*ptr = 0;cout << *ptr << endl;return 0; }

Output:

輸出:

main.cpp: In function ‘int main()’: main.cpp:7:5: error: invalid use of ‘this’ in non-member functionthis* ptr;^~~~ main.cpp:7:11: error: ‘ptr’ was not declared in this scopethis* ptr;^~~

Explanation:

說明:

The code will generate an error because we cannot use this pointer outside the class. because this pointer points to the current object inside the class.

該代碼將產(chǎn)生錯誤,因為我們不能在類外部使用此指針 。 因為此指針指向類內(nèi)的當(dāng)前對象。

Program 2:

程式2:

#include <iostream> using namespace std;class Test {int T1;public:Test(){this* ptr;*ptr = &T1;cout << *ptr;} };int main() {Test T;return 0; }

Output:

輸出:

main.cpp: In constructor ‘Test::Test()’: main.cpp:10:15: error: ‘ptr’ was not declared in this scopethis* ptr;^~~

Explanation:

說明:

The above code will generate an error because this is the default pointer of the current object we can access members of the class inside the class. But we cannot create pointers using this.

上面的代碼將產(chǎn)生一個錯誤,因為這是當(dāng)前對象的默認(rèn)指針,我們可以在該類內(nèi)部訪問該類的成員。 但是我們不能使用this創(chuàng)建指針。

In the above program, we created pointers using this inside the constructor, which is not correct.

在上面的程序中,我們在構(gòu)造函數(shù)內(nèi)部使用此指針創(chuàng)建了指針,這是不正確的。

Program 3:

程式3:

#include <iostream> using namespace std;class Test {int T1;public:Test(){T1 = 10;cout << this->T1;} };int main() {Test T;return 0; }

Output:

輸出:

10

Explanation:

說明:

Here, we created a class Test that contains data member T1 and we defined a default constructor inside the class Test.

在這里,我們創(chuàng)建了一個包含數(shù)據(jù)成員 T1的 Test類,并且在Test類中定義了一個默認(rèn)構(gòu)造函數(shù) 。

In the constructor, we assign value 10 to the T1 and print using the below statement.

在構(gòu)造函數(shù)中,我們將值10分配給T1并使用以下語句進行打印。

cout<<this->T1;

In the above statement, we accessed T1 using this, because the this is a pointer to the current object. Thus, it will print 10 on the console screen.

在上面的語句中,我們使用this來訪問T1 ,因為this是指向當(dāng)前對象的指針。 因此,它將在控制臺屏幕上打印10。

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

c語言指針++

總結(jié)

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

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