-2

Let's say we have two classes:

class A:
{
   A(int a); // c'tor
   virtual ~A(); // d'tor
   FirstMethod(...);
   SecondMethod(...); 
}

class B:public A
{
   B(int a); // c'tor
   ~B(); // d'tor
   FirstMethod(...); 
   SecondMethod(...);
}

The two classes A and B have the exact same methods(same name and parameters but not necessarily the same functionality) and members, except for the destructor's name and the constructor's name, which are different. Now, let's say we have an object:

 A* aObject = new A();

and we do:

B* bObject= dynamic_cast<B*>(aObject);

Would the last casting succeed or that bObject will be NULL? or in other words, can the program distinguish between objects that are A* type and objects that are B* type?

  • You can always check if it's a bad pointer or catch `std::bad_cast`. So I'd rephrase your question. Another side note, your code doesn't compile. Destructors cannot have arguments. –  Jan 23 '14 at 02:06
  • My bad on answering without thinking, and it is deleted. In fact, `dynamic_cast` is not related with 'having same base class'. It is used to cast pointers from base class to derived class, not between derived classes. – xis Jan 23 '14 at 02:07
  • @Xiaoge Su, question is edited, B was meant to be derived from A – user2750466 Jan 23 '14 at 02:18
  • @user2750466 Have you tried to put the d'tor of A `virtual`? You need the vptr to make `dynamic_cast` work. – xis Jan 23 '14 at 02:19
  • `~A(int a)` ??? Post a real testcase, please, not made up nonsense – Lightness Races in Orbit Jan 23 '14 at 02:21
  • @ Lightness Races in Orbit, You are right, My fault, I didn't pay attention to that! edited. – user2750466 Jan 23 '14 at 02:33
  • You're still missing semicolons and that. Please compile the testcase before posting it. – Lightness Races in Orbit Jan 23 '14 at 02:54

2 Answers2

4

Would the last casting succeed or that bObject will be NULL?

It would be NULL. Dynamic casting does not magically create a B. It only gives you a B* if you have an actual B to point to. Here, you only have a A.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • I know it's wierd, I don't know why but I thought the processor can't tell the difference bewteen A and B, because they are exactly the same except for the names (and the names of c'tor and d'tor). So, in that case when B is derived from A, (A has a virtual method), would the dynamic_cast return value be NULL?? – user2750466 Jan 23 '14 at 02:29
  • @user2750466: If your computer can't tell the difference between an `A` and a `B` then you're in a whole heap of trouble... – Lightness Races in Orbit Jan 23 '14 at 02:53
1

The dynamic cast will fail, but the compile will fail first. I know your intent, but I think you're trying to write dynamic_cast(aObject) instead of trying to cast the type. This is the compile failure. But after getting that corrected, the dynamic cast will still fail because aObject is not of type B* (it fails the is-a test). For it to work, B would have to be derived from A.

Paul Dardeau
  • 2,589
  • 1
  • 13
  • 10
  • B is derived from A, but my question is if it was A, and we are trying to cast it to B, would it succeed or fail? – user2750466 Jan 23 '14 at 02:13
  • No, B is not derived from A. That may have been your intention, but that's not the case based on your code snippet. For your above comment, no the cast would still fail as A is not a B, even though B might be an A. – Paul Dardeau Jan 23 '14 at 02:16
  • 2
    The dynamic cast would still fail because an instance of A alone is not of type B. – Paul Dardeau Jan 23 '14 at 02:18
  • @ Paul Dardeau, that's what I was looking for, A also has a virtual method. it might be a little bit wierd but I thought the program won't distinguish between A and B – user2750466 Jan 23 '14 at 02:21
  • @user2750466 It will not throw a `bad_cast` per se, but `bObject` will not be a valid pointer. –  Jan 23 '14 at 02:22
  • @ remyabel So, would the dynamic_cast return value be NULL? – user2750466 Jan 23 '14 at 02:24
  • 1
    @user2750466 yes, dynamic_cast returns NULL if the cast was not successful, or non-NULL if it succeeded. – Paul Dardeau Jan 23 '14 at 03:22