944,047 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2454
  • C RSS
Jul 8th, 2006
0

downcasting and dynamic_cast

Expand Post »
I've gone through several books they explain Upcasting as
Base class pointer pointing to Drived class Object

this definition is correct.
Downcasting:
Quote ...
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 .
Similar Threads
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Jul 8th, 2006
0

Re: downcasting and dynamic_cast

Hmm. Clues here: http://www.gotw.ca/gotw/017.htm ?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Finish my DATABASE using C
Next Thread in C Forum Timeline: swapping





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC