Dynamic cast is correct. You're obviously not doing what you say you are.
this works for me:
#include <iostream>
class Base
{
public:
virtual void foo(){}
};
class Mixin
{
};
class Middle : public Base, public Mixin
{
};
class Der : public Middle
{
};
using std::cout;
using std::cin;
int main()
{
Base* base = new Der();
Middle* middle = dynamic_cast<Middle*>(base);
Der* der1 = dynamic_cast<Der*>(base);
Der* der2 = dynamic_cast<Der*>(middle);
Der* der3 = static_cast<Der*>(base);
cout << der1 << '\n';
cout << der2 << '\n';
cout << der3 << '\n';
cin.get();
}
#include <iostream>
class Base
{
public:
virtual void foo(){}
};
class Mixin
{
};
class Middle : public Base, public Mixin
{
};
class Der : public Middle
{
};
using std::cout;
using std::cin;
int main()
{
Base* base = new Der();
Middle* middle = dynamic_cast<Middle*>(base);
Der* der1 = dynamic_cast<Der*>(base);
Der* der2 = dynamic_cast<Der*>(middle);
Der* der3 = static_cast<Der*>(base);
cout << der1 << '\n';
cout << der2 << '\n';
cout << der3 << '\n';
cin.get();
}
To copy to clipboard, switch view to plain text mode
By the way, 'up casting' is sometimes a sign of missing/poor design.
Bookmarks