Created
September 25, 2013 14:45
-
-
Save fresky/6700691 to your computer and use it in GitHub Desktop.
example to show the difference between dynamic cast and c style cast.
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 BaseA | |
{ | |
public: | |
virtual void foo()=0; | |
}; | |
class BaseB | |
{ | |
public: | |
virtual void bar(int a)=0; | |
}; | |
class Child: public BaseA, public BaseB | |
{ | |
public: | |
void foo() | |
{ | |
cout<<"i'm foo in Child!"<<endl; | |
}; | |
void bar(int a) | |
{ | |
cout<<"i'm bar in Child!"<<endl; | |
}; | |
}; |
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
int main() { | |
BaseB* b = new Child(); | |
BaseA* a = (BaseA*)b; | |
BaseA* a2 = dynamic_cast<BaseA*> (b); | |
// This is actually calling bar(), | |
// and will cause Runtime check failure about ESP if the foo and bar have different signature. | |
a->foo(); | |
a2->foo(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment