Cpp クラス 継承 デストラクタ 新しいページはコチラ

提供: yonewiki
移動: 案内, 検索
(クラス 継承 デストラクタ)
 
5行: 5行:
 
<tr>
 
<tr>
 
<td style="width:1px;"></td>
 
<td style="width:1px;"></td>
<td class="mbox-text plainlist" style="">本来の表記は「<b><span id="RealTitle" style="font-size:large;">C++ クラス 継承 デストラクタ</span></b>」です。この記事に付けられた題名は{{記事名の制約}}から不正確なものとなっています。</td>
+
<td class="mbox-text plainlist" style="">本来の表記は「<b><span id="RealTitle" style="font-size:large;">C++(Cpp) クラス 継承 デストラクタ</span></b>」です。この記事に付けられた題名は{{記事名の制約}}から不正確なものとなっています。</td>
 
</tr>
 
</tr>
 
</table>
 
</table>
49行: 49行:
 
</syntaxhighlight2>
 
</syntaxhighlight2>
  
 +
 16行目の
 +
 +
<syntaxhighlight2 lang="cpp">
 +
virtual ~CBaseInheritance();
 +
</syntaxhighlight2>
 
 これだけで、基底クラスが消滅したときにアップキャストされた基底クラスが派生クラスのデストラクタを保持することになります。つまり派生クラスのデストラクタが動く可能性が出てきました。しかし、今回作ったメイン関数では、ポリモーフィズムで形態を変えるごとに基底クラスが消滅しないため、1回目の派生クラスからのアップキャスト生成と2回目のアップキャスト生成に対しては消滅処理がされないため、派生クラスのデストラクタは呼ばれません。基底クラスのデストラクタも呼ばれません。
 
 これだけで、基底クラスが消滅したときにアップキャストされた基底クラスが派生クラスのデストラクタを保持することになります。つまり派生クラスのデストラクタが動く可能性が出てきました。しかし、今回作ったメイン関数では、ポリモーフィズムで形態を変えるごとに基底クラスが消滅しないため、1回目の派生クラスからのアップキャスト生成と2回目のアップキャスト生成に対しては消滅処理がされないため、派生クラスのデストラクタは呼ばれません。基底クラスのデストラクタも呼ばれません。
  
58行: 63行:
 
Constructor:CBaseInheritance(int,int)
 
Constructor:CBaseInheritance(int,int)
 
Constructor:CDriveInheritance(int,int)
 
Constructor:CDriveInheritance(int,int)
BaseMoney=34800
+
BaseMoney=34800, total=35700
 
Constructor:CBaseInheritance(int,int)
 
Constructor:CBaseInheritance(int,int)
 
Constructer:CDeriveArrInheritance(int,int,int*,int)
 
Constructer:CDeriveArrInheritance(int,int,int*,int)
BaseMoney=34800
+
BaseMoney=34800, total=36520
 
Constructor:CBaseInheritance(int,int)
 
Constructor:CBaseInheritance(int,int)
 
Constructer:CDeriveDiscountInheritance(int,int,double)
 
Constructer:CDeriveDiscountInheritance(int,int,double)
BaseMoney=46800
+
BaseMoney=46800, total=23400
 
Destructer:~CDeriveDiscountInheritance()
 
Destructer:~CDeriveDiscountInheritance()
 
Destructor:~CBaseInheritance()
 
Destructor:~CBaseInheritance()
96行: 101行:
 
     CBaseInheritance* pCBaseInheritanceUpcast;
 
     CBaseInheritance* pCBaseInheritanceUpcast;
 
     pCBaseInheritanceUpcast = pCBaseInheritanceUpcastF(1);
 
     pCBaseInheritanceUpcast = pCBaseInheritanceUpcastF(1);
     pCBaseInheritanceUpcast->mf_vBaseDispValue();
+
     pCBaseInheritanceUpcast->mf_vDispValue();
 
     delete pCBaseInheritanceUpcast;
 
     delete pCBaseInheritanceUpcast;
  
 
     pCBaseInheritanceUpcast = pCBaseInheritanceUpcastF(2);
 
     pCBaseInheritanceUpcast = pCBaseInheritanceUpcastF(2);
     pCBaseInheritanceUpcast->mf_vBaseDispValue();
+
     pCBaseInheritanceUpcast->mf_vDispValue();
 
     delete pCBaseInheritanceUpcast;
 
     delete pCBaseInheritanceUpcast;
  
 
     pCBaseInheritanceUpcast = pCBaseInheritanceUpcastF(3);
 
     pCBaseInheritanceUpcast = pCBaseInheritanceUpcastF(3);
     pCBaseInheritanceUpcast->mf_vBaseDispValue();
+
     pCBaseInheritanceUpcast->mf_vDispValue();
 
     delete pCBaseInheritanceUpcast;
 
     delete pCBaseInheritanceUpcast;
  
118行: 123行:
 
Constructor:CBaseInheritance(int,int)
 
Constructor:CBaseInheritance(int,int)
 
Constructor:CDriveInheritance(int,int)
 
Constructor:CDriveInheritance(int,int)
BaseMoney=34800
+
BaseMoney=34800, total=35700
 
Destructer:~CDeriveInheritance()
 
Destructer:~CDeriveInheritance()
 
Destructor:~CBaseInheritance()
 
Destructor:~CBaseInheritance()
 
Constructor:CBaseInheritance(int,int)
 
Constructor:CBaseInheritance(int,int)
 
Constructer:CDeriveArrInheritance(int,int,int*,int)
 
Constructer:CDeriveArrInheritance(int,int,int*,int)
BaseMoney=34800
+
BaseMoney=34800, total=36520
 
Destructer:~CDeriveArrInheritance()
 
Destructer:~CDeriveArrInheritance()
 
Destructor:~CBaseInheritance()
 
Destructor:~CBaseInheritance()
 
Constructor:CBaseInheritance(int,int)
 
Constructor:CBaseInheritance(int,int)
 
Constructer:CDeriveDiscountInheritance(int,int,double)
 
Constructer:CDeriveDiscountInheritance(int,int,double)
BaseMoney=46800
+
BaseMoney=46800, total=23400
 
Destructer:~CDeriveDiscountInheritance()
 
Destructer:~CDeriveDiscountInheritance()
 
Destructor:~CBaseInheritance()
 
Destructor:~CBaseInheritance()
 
</syntaxhighlight2>
 
</syntaxhighlight2>
  
 という感じで、きちんとそれぞれの派生クラスのデストラクタが動くようになります。よくクラスのデストラクタにはvirtualを付けとけ、というようなことを言う、なんでもvirtual ~xxxデストラクタ主義を唱える人がいますが、自分はアップキャストで使うクラスだけに付ければよいと思います。そんなアップキャストするかどうかも意識していない基底クラスを利用すれば、デストラクタがどこではたくかを意識していないということになるので、雑なプログラムになってしまいます。ちゃんと管理した方が良いと思います。設計図でしっかりと管理してほしいな。なんでもvirtualでもいいですけど。不要なデストラクタが省略できなくなるよ。
+
 という感じで、きちんとそれぞれの派生クラスのデストラクタが動くようになります。よくクラスのデストラクタにはvirtualを付けとけ、というようなことを言う、なんでもvirtual ~xxxデストラクタ主義を唱える人がいますが、自分はアップキャストで使うクラスだけに付ければよいと思います。そんなアップキャストするかどうかも意識していない基底クラスを利用すれば、デストラクタがどこではたらくかを意識していないということになるので、雑なプログラムになってしまいます。ちゃんと管理した方が良いと思います。設計図でしっかりと管理してほしいな。なんでもvirtualでもいいですけど。不要なデストラクタが省略できなくなるよ。
 +
 
  
 
 それってわかりにくくないかな。
 
 それってわかりにくくないかな。
 +
  
 
 好きなようにしてもらえればいいですけど。
 
 好きなようにしてもらえればいいですけど。
 +
  
 
[[C PlusPlus#C++からの技術|C++]]に戻る
 
[[C PlusPlus#C++からの技術|C++]]に戻る

2021年2月6日 (土) 00:00時点における最新版



個人用ツール
名前空間

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