#include <iostream>
usingnamespace std;
class A{ };
class B:public A{};
int main()
{
A a,*x;
B b,*y;
y = dynamic_cast<B *>(&a);
if(x)
{
cout << "success in dynamic cast" << endl;
}else{
cout << "unsuccessful dynamic casting" << endl;
}
return 0;
}
---------------------------------------
# g++ test1.cpp
test1.cpp: In function ‘int main()’:
test1.cpp:13:26: error: cannot dynamic_cast ‘& a’ (of type ‘class A*’) to type ‘class B*’ (source type is not polymorphic)
#include <iostream>
usingnamespace std;
class A{ virtualvoid test(){}};
class B:public A{};
int main()
{
A a,*x;
B b,*y;
y = dynamic_cast<B *>(&a);
if(x)
{
cout << "success in dynamic cast" << endl;
}else{
cout << "unsuccessful dynamic casting" << endl;
}
return 0;
}
# g++ test1.cpp
test1.cpp: In function ‘int main()’:
test1.cpp:13:26: warning: dynamic_cast of ‘A a’ to ‘class B*’ can never succeed
if dynamic_cast does not succeed in this case, why does it only gives warning?
doesn't it violate the definition of dynamic_cast (where in dynamic_cast casting should succeed completely unlike static_cast).
~
'A' dont have any B's part. so it cannot be casted. A 'B' can be casted to 'A' and back to 'B'.
When you create A's object, it will not have anything which B contains and hence when you cast it to B, the B's part will not be there. Also, polymorphism will work with pointers and references.
The compiler will not stop you from doing anything stupid things. It will just warn you.
As stated above, variable a can never be casted as class B because it is class A. But the specific compiler warning comes from the fact that class A doesn't have any virtual members. The dynamic_cast operator requires that the classes have at least one virtual member so RTTI information is generated.
Yes, but the compiler is complaining about class A not being being polymorphic. Re-read the error message. It says "source type is not polymorphic", "source" being the key word here.
Yes, but the compiler is complaining about class A not being being polymorphic. Re-read the error message.
That's actually not the error message for the second block of code ;P. I think you and writetonsharma are talking about 2 different things.
anandnilkal wrote:
if dynamic_cast does not succeed in this case, why does it only gives warning?
Because the syntax of what you're trying to do is perfectly legal. There's no reason for the compiler to say it's an error.
However the compiler recognizes that logically what you're trying to do makes no sense, so it tells you by giving you a warning.
Basically... it's a logic error. Not a syntax error. The compiler only gives you errors for syntax errors.
doesn't it violate the definition of dynamic_cast (where in dynamic_cast casting should succeed completely unlike static_cast).
That's not the definition of dynamic_cast at all. dynamic_cast can fail, just when it does fail, it will be a "safe" failure in that you'll get a null pointer instead of a bad pointer.
@writetonsharma, @webjose and @Disch thanks for your comments.
Yes, i understand the error is first case. i am bit confused in the second case.
as @writetonsharma wrote
'A' dont have any B's part. so it cannot be casted. A 'B' can be casted to 'A' and back to 'B
this is true for any case (let there be member variables and functions or not).
why one virtual method declaration in base could possibly move an error condition to a mere warning?
as compiler has rightely indicated in second case,this would never succeed. there are other kinds of casting mechanism which could be used to achieve this ex: static_cast, where responsibility is solely with the developer and he has handle the consequences of it.
my argument is solely based on the fact that dynamic_cast is meant to ensure complete casting.