Originally Posted by
McToo
This line will call the base class implementation because of the static_cast<GS_object*> call. static_cast will convert a pointer to a base class pointer.
But that method is virtual, so cast shouldn't change anything.
#include <iostream>
class A
{
public:
virtual void foo() { std::cerr << "A::foo()" << std::endl; }
};
class B : public A
{
public:
virtual void foo() { std::cerr << "B::foo()" << std::endl; }
void bar() { A::foo(); }
};
int main()
{
A a;
B b;
std::cerr << "a.foo() :\t";
a.foo();
std::cerr << "b.foo() :\t";
b.foo();
std::cerr << "static_cast :\t";
( static_cast< A* >( &b ) )->foo();
std::cerr << "dynamic_cast :\t";
( dynamic_cast< A* >( &b ) )->foo();
std::cerr << "b.bar() :\t";
b.bar();
return 0;
}
#include <iostream>
class A
{
public:
virtual void foo() { std::cerr << "A::foo()" << std::endl; }
};
class B : public A
{
public:
virtual void foo() { std::cerr << "B::foo()" << std::endl; }
void bar() { A::foo(); }
};
int main()
{
A a;
B b;
std::cerr << "a.foo() :\t";
a.foo();
std::cerr << "b.foo() :\t";
b.foo();
std::cerr << "static_cast :\t";
( static_cast< A* >( &b ) )->foo();
std::cerr << "dynamic_cast :\t";
( dynamic_cast< A* >( &b ) )->foo();
std::cerr << "b.bar() :\t";
b.bar();
return 0;
}
To copy to clipboard, switch view to plain text mode
$ ./a.out
a.foo() : A::foo()
b.foo() : B::foo()
static_cast : B::foo()
dynamic_cast : B::foo()
b.bar() : A::foo()
$ ./a.out
a.foo() : A::foo()
b.foo() : B::foo()
static_cast : B::foo()
dynamic_cast : B::foo()
b.bar() : A::foo()
To copy to clipboard, switch view to plain text mode
Bookmarks