downcasting and dynamic_cast

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

downcasting and dynamic_cast

 
0
  #1
Jul 8th, 2006
I've gone through several books they explain Upcasting as
Base class pointer pointing to Drived class Object

this definition is correct.
Downcasting:
Drived class pointer pointing to base class object..
i got a bit confused.....not having crystal clear image in my mind.....
if the above definition is correct... whats wrong with the below code

  1. class Shape {
  2. public:
  3. virtual ~Shape() {}
  4. };
  5. class Circle: public Shape {
  6. public:
  7. ~Circle() {}
  8. };
  9.  
  10. int main()
  11. {
  12. //why this can't work ...........according to definition....
  13. // Circle* c = dynamic_cast<Shape*>(new Shape);
  14.  
  15. //why this
  16. Shape* s = new Shape;
  17. Circle* c = dynamic_cast<Circle*>(s);
  18. return 0;
  19. }

I know that downcasting is performed between polymorphic types . I m bit confused with its definition and implementation... please give me the solid concept .
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: downcasting and dynamic_cast

 
0
  #2
Jul 8th, 2006
Hmm. Clues here: http://www.gotw.ca/gotw/017.htm ?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum


Views: 1599 | Replies: 1
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC