P
pietromas
In the example below, why does the dynamic_cast fail (return NULL)? It
should be able to cast between sibling classes ...
#include <iostream>
class A
{
public:
virtual const int get() const = 0;
};
class B : public A
{
public:
virtual const int get() const { return 0; }
};
class C : public A
{
public:
virtual const int get() const { return 1; }
};
int main()
{
A *a;
a = new B();
std::cout << a->get() << std::endl;
a = dynamic_cast<C*>(a);
if(a)
std::cout << a->get() << std::endl;
return 0;
}
should be able to cast between sibling classes ...
#include <iostream>
class A
{
public:
virtual const int get() const = 0;
};
class B : public A
{
public:
virtual const int get() const { return 0; }
};
class C : public A
{
public:
virtual const int get() const { return 1; }
};
int main()
{
A *a;
a = new B();
std::cout << a->get() << std::endl;
a = dynamic_cast<C*>(a);
if(a)
std::cout << a->get() << std::endl;
return 0;
}