-1

I have a very general question, related to dynamic_cast in c++: Let's assume we have the following classes:

class Father{ 
public:
       Father(...); // this is constructor
       //.....
       //.. Whatever code in here, not important
       ~ Father(); //destructor 
protected:
      // some protected variables
} // end of class Father


class Son:public Father {
public:
       Son(...); // this is cnostructor
       //.....
       //.. Whatever code in here, not important
       ~ Son(); //destructor 
protected:
      // some protected variables
} // end of class Son


 class Another{
 public:
      Another(...); // this is constructor
       //.....
       //.. Whatever code in here, not important
       ~ Another(); //destructor 

       AnotherMethod(Father* fatherOpj,......) //

} // end of class Another          

And let's say the method "AnotherMethod" do the next:

   AnotherMethod(Father* fatherOpj,......)
   { 
      Son *sonObj = dynamic_cast<Son*>(fatherOpj);
    // using sonObj
   }

While in the main we do the following:

  Son* son1=Null;
  //...
  son1 = new Son(.....);
 //....
  AnotherMethod(son1,....);

Now, I'm wondering about the next issue: that way- when we call AnotherMethod with a pointer of type Son*, while in its signature there's Father*- will we lose the fields/members in son (which father doesn't have) or that by doing the dynamic_cast we will be able to have them (with the right values inside them)?!

  • 1
    You will be able to use data members and member functions of `Son` with `sonObj->x`. However, unless this is a contrived example, `Son` probably shouldn't derive from `Father`. Also note that you're not checking if the `dynamic_cast` returned a null pointer. – Simple Jan 09 '14 at 14:37
  • 1
    Please note that `dynamic_cast` won't work without at least one virtual function, usually the base class destructor. – Sean Jan 09 '14 at 14:38
  • @Simple in my own code, I do the casting only if I know for sure that the parameter I get (father* fatherOpj) is a son*, in that case, is there a possibility for null?? – user2750466 Jan 09 '14 at 14:45
  • @user2750466 no, but in that case you shouldn't be using `dynamic_cast` but `static_cast` instead. – Simple Jan 09 '14 at 14:45
  • @Simple why? is it bad? what would that change if it was static? – user2750466 Jan 09 '14 at 14:48
  • @user2750466 `dynamic_cast` incurs overhead because it has to check whether the object is really the type you're casting it to. If you already know that this is true then you don't need this check, which `static_cast` omits. – Simple Jan 09 '14 at 14:51
  • @Simple I'm not that good in c++, so I don't get it, what will dynamic be harmful to? and will both dynamic and static do the job? – user2750466 Jan 09 '14 at 14:56
  • 1
    @user2750466: If, as you say, you know *for sure* that the dynamic type is `Son`, then `dynamic_cast` will be doing unnecessary work. `static_cast` would be more efficient, and would work for non-polymorphic types. But it will go horribly wrong if the dynamic type actually isn't `Son`, so `dynamic_cast` is safer if you have the slightest doubt in your assumption. – Mike Seymour Jan 09 '14 at 15:06

2 Answers2

3

If the pointer really points to a Son object (i.e. the dynamic type is Son, while the static type is Father), then all of the Son fields are there, and are accessible after casting the pointer.

If it doesn't point to Son, then the cast will fail, giving a null pointer.

Note that dynamic_cast only works if Father is polymorphic - that is, if it declares at least one virtual function. In your example, it isn't polymorphic, so the cast will fail.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • And what if the destructor was declared as virtual? will the cast succeed then? – user2750466 Jan 09 '14 at 14:41
  • 1
    @user2750466: Yes, any virtual function, including the destructor, will make it polymorphic. – Mike Seymour Jan 09 '14 at 14:43
  • if I do the casting only if I knew for sure that it points to son, would it be harmful to do dynamic? should I do static in that case? or that both will work? – user2750466 Jan 09 '14 at 15:05
  • 1
    @user2750466: It depends what you mean by "harmful". `dynamic_cast` will be slower, but `static_cast` will go horribly wrong if your assumption turns out to be wrong. – Mike Seymour Jan 09 '14 at 15:07
1

will we lose the fields/members in son

No, you just can't access it from pointer to Father.

by doing the dynamic_cast we will be able to have them (with the right values inside them)

Yes, you will, because it's still a Son object (if passed object has Son type, of course).

awesoon
  • 32,469
  • 11
  • 74
  • 99