warning in dynamic_cast while casting from base class to derived class

HI,

test1.cpp
----------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace 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)

test1.cpp
------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

class A{ virtual void 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.
webjose, he has got a virtual in the second code. I also missed that in the beginning.
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.
Last edited on
webJose wrote:
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.
exactly Disch, I am talking about the second code fragment and webjose is talking about the first one. :)
Ah, ok. I see. Yes, my bad. I got stuck in the very first error message and the problem is not there in the second block.
Hi,

@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.
Topic archived. No new replies allowed.