Skip to content

Instantly share code, notes, and snippets.

@fresky
Created September 25, 2013 14:45
Show Gist options
  • Save fresky/6700691 to your computer and use it in GitHub Desktop.
Save fresky/6700691 to your computer and use it in GitHub Desktop.
example to show the difference between dynamic cast and c style cast.
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;
};
};
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