Cpp クラス thisポインタ 新しいページはコチラ
提供: yonewiki
(→クラス thisポインタ) |
(→クラス thisポインタ) |
||
85行: | 85行: | ||
自動的に作られるthisというキーワードのポインタ変数は、意図的にthisポインタと同じものを作ることができます。 | 自動的に作られるthisというキーワードのポインタ変数は、意図的にthisポインタと同じものを作ることができます。 | ||
+ | |||
+ | |||
+ | thisポインタと同じようなものを作るという意味で、プログラムを継ぎ足し変更したりして、以下のように変えてみます。 | ||
+ | |||
+ | |||
+ | ThisPointer001.h | ||
+ | <syntaxhighlight lang="cpp" line start="1"> | ||
+ | #pragma once | ||
+ | class CThisPointer001 | ||
+ | { | ||
+ | public: | ||
+ | int mpub_nValue; | ||
+ | CThisPointer001* this2; | ||
+ | void mfpub_SetThis(CThisPointer001* pCThisPointer001_this); | ||
+ | void mfpub_Output1(void); | ||
+ | |||
+ | CThisPointer001(void); | ||
+ | ~CThisPointer001(void); | ||
+ | }; | ||
+ | |||
+ | </syntaxhighlight> | ||
+ | ThisPointer001.cpp | ||
+ | <syntaxhighlight lang="cpp" line start="1"> | ||
+ | #include "stdafx.h" | ||
+ | #include "ThisPointer001.h" | ||
+ | |||
+ | void CThisPointer001::mfpub_SetThis(CThisPointer001* pCThisPointer001_this){ | ||
+ | printf("\n"); | ||
+ | printf("■CThisPointer001::mfpub_SetThis\n"); | ||
+ | |||
+ | printf("pCThisPointer001_this Address =%08x\n",pCThisPointer001_this); | ||
+ | this2 = pCThisPointer001_this; | ||
+ | printf("this Address =%08x\n",this); | ||
+ | printf("this2 Address =%08x\n",this2); | ||
+ | |||
+ | pCThisPointer001_this->mpub_nValue = 10; | ||
+ | printf("mpub_nValue =%08d\n",mpub_nValue); | ||
+ | |||
+ | this->mpub_nValue = 100; | ||
+ | printf("mpub_nValue =%08d\n",mpub_nValue); | ||
+ | |||
+ | mpub_nValue = 2000; | ||
+ | printf("mpub_nValue =%08d\n",mpub_nValue); | ||
+ | |||
+ | } | ||
+ | void CThisPointer001::mfpub_Output1(){ | ||
+ | printf("\n"); | ||
+ | printf("■CThisPointer001::mfpub_Output1\n"); | ||
+ | |||
+ | printf("this Address =%08x\n",this); | ||
+ | printf("this2 Address =%08x\n",this2); | ||
+ | printf("mpub_nValue =%08d\n",mpub_nValue); | ||
+ | |||
+ | } | ||
+ | |||
+ | CThisPointer001::CThisPointer001(void) | ||
+ | { | ||
+ | printf("\n"); | ||
+ | printf("■CThisPointer001コンストラクタ\n"); | ||
+ | printf("this Address =%08x\n",this); | ||
+ | } | ||
+ | |||
+ | |||
+ | CThisPointer001::~CThisPointer001(void) | ||
+ | { | ||
+ | } | ||
+ | |||
+ | </syntaxhighlight> | ||
+ | Sample_main.cpp | ||
+ | <syntaxhighlight lang="cpp" line start="1"> | ||
+ | #include "stdafx.h" | ||
+ | #include "ThisPointer001.h" | ||
+ | |||
+ | int _tmain(int argc, _TCHAR* argv[]) | ||
+ | { | ||
+ | CThisPointer001* CThisPointer001_Instance = new CThisPointer001; | ||
+ | |||
+ | CThisPointer001_Instance->mfpub_SetThis(CThisPointer001_Instance); | ||
+ | CThisPointer001_Instance->mfpub_Output1(); | ||
+ | |||
+ | CThisPointer001_Instance->mpub_nValue = 1000; | ||
+ | |||
+ | printf("\n"); | ||
+ | printf("■Main\n"); | ||
+ | printf("CThisPointer001_Instance Address=%08x\n",CThisPointer001_Instance); | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | </syntaxhighlight> |