(Not logged on) | Register | Log On

You can subscribe to this discussion group using an RSS feed reader. techInterview Discussion

Answers to technical interview questions. A part of TechInterview.org

A part of techInterview.org: answers to technical interview questions.

Your host: Michael Pryor

Own dynamic casr in C++

How will you write your own dynamic cast which does the same
work as the dynamic cast which C++ provides.
Coder Send private email
Wednesday, July 07, 2010
 
 
class base {
  public :
    virtual der1* isDer1 () = 0 ;
    virtual der2* isDer2 () = 0 ;
};

class der1 : public base {
  public :
    der1* isDer1 () { return this; }
    der2* isDer2 () { return 0; }
};

class der2 : public base {
  public :
    der1* isDer1 () { return 0; }
    der2* isDer2 () { return this; }
};
puneet Send private email
Wednesday, July 07, 2010
 
 
Dynamica cast is for downcasting . I am not sure how Puneet's solution will work. I thought that something related to typeid would work.

Dyncamic cast will return NULL if I try to convert B* To D* if D is not derived from B.
Mahesh Send private email
Wednesday, July 07, 2010
 
 
Agree that typeid is probably the only reasonable way to do this.
d Send private email
Wednesday, July 07, 2010
 
 
D ; Can you give me an example to do that using typeid.
Coder Send private email
Thursday, July 08, 2010
 
 
I thought typeid gave you access to the inheritance DAG, but it doesn't. You'd have to construct that out-of-band.
d Send private email
Thursday, July 08, 2010
 
 
Can somebody come up with any kind of solution for this by writing a sample code
Coder Send private email
Friday, July 09, 2010
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz