Cpp クラス thisポインタ 新しいページはコチラ

提供: yonewiki
移動: 案内, 検索
(クラス thisポインタ)
(クラス thisポインタ)
96行: 96行:
 
{
 
{
 
public:
 
public:
   int mpub_nValue;
+
   int mpub_nValue;//★1
   CThisPointer001* this2;
+
   CThisPointer001* this2;//★2
   void mfpub_SetThis(CThisPointer001* pCThisPointer001_this);
+
   void mfpub_SetThis(CThisPointer001* pCThisPointer001_this);//★3
   void mfpub_Output1(void);
+
   void mfpub_Output1(void);//★4
  
 
   CThisPointer001(void);
 
   CThisPointer001(void);
111行: 111行:
 
#include "ThisPointer001.h"
 
#include "ThisPointer001.h"
  
 +
//★5
 
void CThisPointer001::mfpub_SetThis(CThisPointer001* pCThisPointer001_this){
 
void CThisPointer001::mfpub_SetThis(CThisPointer001* pCThisPointer001_this){
 
   printf("\n");
 
   printf("\n");
120行: 121行:
 
   printf("this2 Address                  =%08x\n",this2);
 
   printf("this2 Address                  =%08x\n",this2);
  
   pCThisPointer001_this->mpub_nValue = 10;
+
   pCThisPointer001_this->mpub_nValue = 10;//★10.
 
   printf("mpub_nValue                    =%08d\n",mpub_nValue);
 
   printf("mpub_nValue                    =%08d\n",mpub_nValue);
  
   this->mpub_nValue = 100;
+
   this->mpub_nValue = 100;//★11.
 
   printf("mpub_nValue                    =%08d\n",mpub_nValue);
 
   printf("mpub_nValue                    =%08d\n",mpub_nValue);
  
   mpub_nValue = 2000;
+
   mpub_nValue = 2000;//★12.
 
   printf("mpub_nValue                    =%08d\n",mpub_nValue);
 
   printf("mpub_nValue                    =%08d\n",mpub_nValue);
  
 
}
 
}
 +
 +
//★6
 
void CThisPointer001::mfpub_Output1(){
 
void CThisPointer001::mfpub_Output1(){
 
   printf("\n");
 
   printf("\n");
162行: 165行:
 
   CThisPointer001* CThisPointer001_Instance = new CThisPointer001;
 
   CThisPointer001* CThisPointer001_Instance = new CThisPointer001;
  
   CThisPointer001_Instance->mfpub_SetThis(CThisPointer001_Instance);
+
   CThisPointer001_Instance->mfpub_SetThis(CThisPointer001_Instance);//★100
 
   CThisPointer001_Instance->mfpub_Output1();
 
   CThisPointer001_Instance->mfpub_Output1();
  
175行: 178行:
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
そうすると、出力結果は以下のようになります。
 +
<syntaxhighlight lang="text">
 +
■CThisPointer001コンストラクタ
 +
this Address                    =0094eeb8
 +
 +
■CThisPointer001::mfpub_SetThis
 +
pCThisPointer001_this Address  =0094eeb8
 +
this Address                    =0094eeb8
 +
this2 Address                  =0094eeb8
 +
mpub_nValue                    =00000010
 +
mpub_nValue                    =00000100
 +
mpub_nValue                    =00002000
 +
 +
■CThisPointer001::mfpub_Output1
 +
this Address                    =0094eeb8
 +
this2 Address                  =0094eeb8
 +
mpub_nValue                    =00002000
 +
 +
■Main
 +
CThisPointer001_Instance Address=0094eeb8
 +
</syntaxhighlight>
 +
という具合です。
 +
 +
 +
具体的に変更したところの中で重要なところはThisPointer001.hファイルの★1~★4を追加したこと、★5と★6のメンバ関数をプログラムとして追加したことです。
 +
 +
 +
そして、特に確認しておきたいのは★5の関数の引数として、CThisPointer001クラスのメンバ関数の引数として、CThisPointer001クラスのポインタ変数を受け取ることになっている点です。このように自分で自分を受け取るみたいな形でポインタ変数を引数とすることにより、まさにthisポインタと同じようなものを作ったことになります。もちろん、呼び出すときにはメインプログラム側で、実体のある自分自身を引数に受け取らないと、自分自身を示すポインタは作れないので、メインプログラム側の★100のように自分自身を引数にするような組み込みが要求されます。
 +
 +
 +
そうすると、★10のように
 +
 +
 +
pCThisPointer001_this->mpub_nValue = 10;
 +
 +
 +
というような★100で自分自身を受け渡したポインタ変数の中のメンバ変数への代入ができます。クラス内部からのアクセスなので、この代入はプライベートな領域のメンバ変数であっても呼び出すことが出来ます。その確認は自分自身にお任せしたいと思います。
 +
 +
 +
これが自分自身ということになります。★11では、あえてthisポインタを明示したメンバ変数へのアクセス方法を記述しました。★10と同じことをしているというのは、最初のサンプルプログラムで、同じアドレスを保持していたことからも理解してもらえるものと思います。
 +
 +
 +
★12は従来というか、特に意識しなくても自分自身のメンバ変数にアクセスできていることがわかる部分になっています。
 +
 +
 +
thisはポインタと同じようなものを作ったと書きましたが、やはり★12のように省略してもいいし、いつでもthisポインタは使えるという点では、全く同じものを作ることはできません。thisポインタはいつでも使えるという点においては、C++の仕様ですから、この便利さにかなうものもなければ、これと同じ役割のキーワードを新たに作ることも難しいでしょう。常にthisと同じ役割をする別名の変数を作るように動作させるマクロとか作れるのかもしれませんが、それができるかどうかについても自分自身で確かめていただければと思います。それが必要かどうか答えは自分自身で見つけて。
 +
 +
 +
thisポインタが省略できるクラス内でのメンバ関数やメンバ変数の仕組みとかの原理や論理もあると思いますが、そのあたりは自分は理解していません。thisポインタの役割がわかっていれば、プログラムを読み解くことはできると思います。原理や論理を理解しないなんてバカだなぁと思う人はいるかもしれませんが、そういう人と関わるようになったときには積極的に理解していきたいと思います。そのときは優しく教えてくれるといいなぁ。この記事を読まれた若きプログラマは熱意をもって原理や論理にまで追求というか迫ると言うか、そういうようなことをしてもらったほうがいいのかもしれません。

2015年5月28日 (木) 00:00時点における版



個人用ツール
名前空間

変種
操作
案内
ツールボックス