Hello,
few minutes ago i tried to cast dynamically one class into an other class. To be more specific:
I have two Interface. The first Interface inherited to the other one and the second Interface inherited to the two different classes let's say A and B. The second Interface provides one full virtual member function.
The Interfaces:
class Interface
{
public:
Interface(){}
}
class fooInterface
{
public:
fooInterface(){}
virtual int type() =0;
}
The concrete classes A and B:
class A : public fooInterface
{
public:
virtual int type(){return 1;}
virtual void foo(A var){std::cerr<<var.type(); }
}
class B : public fooInterface
{
public:
virtual int type(){return 2;}
virtual void foo(B var){std::cerr<<var.type(); }
}
The main function:
int main()
{
A TestOne;
B TestTwo;
TestOne.foo(*(dynamic_cast<A*>(&TestTwo)));
TestTwo.foo(*(dynamic_cast<B*>(&TestOne)));
return 0;
}
The Output is this:
1
2
Easy to understand and it's buildable. But the compiler give me a warning:
warning: dynamic_cast of 'B TestTwo' to 'class A*' can never succeed [enabled by default]
warning: dynamic_cast of 'A TestOne' to 'class B*' can never succeed [enabled by default]
The problem now is why give me the compiler this warning which says it would be never succed but the programm don't crash here? I came up with this question because i am still on a "bigger" project where i have the same problematic. That is also the reason why i use these two interface classes. the first one is not really in use here in this example, but that's in differen topic.
And I thought before i ask for help for the problem there i want first understand this problem here in generall.