Last active
August 29, 2015 14:07
-
-
Save TSKGunGun/ebc1c1c9075f322fee7e to your computer and use it in GitHub Desktop.
型の変換
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Base{}; | |
class Derived:public Base{}; //Baseクラスの継承 | |
void main(void){ | |
int A(0); | |
double D = double(A); //Cスタイルのキャスト | |
double D2 = static_cast<double>(A); //C++スタイルのキャスト | |
Derived d; | |
Base* pBase = &d; //暗黙の型変換( Derived*をBase*にしている) | |
if( pBase = &d ) return; //型変換前のアドレスと返還後のアドレスが同じとは限らない | |
} | |
class Window | |
{ | |
public: | |
virtual void OnResize(){}; | |
}; | |
class SpecialWindow : public Window | |
{ | |
public: | |
virtual void OnResize() override{ | |
static_cast<Window>(*this).OnResize(); //Windowの一時オブジェクトを作る | |
/* これをやるとWindowの一時オブジェクトを作ってそれにResizeをかける。 | |
自分にはかからないよ!*/ | |
Window::OnResize(); //Window::OnResizeをthisに対して行う | |
} | |
}; | |
Windowのサンプルコード追加。
今まで何も考えないでWindow::OnResize()みたいなことやってたけど、ちゃんと理由がわかりました。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
多重継承を行うと発生しやすいらしい。
コンパイラによって動作が異なる