What is upcasting/downcasting?

Posted by acidzombie24 on Programmers See other posts from Programmers or by acidzombie24
Published on 2012-05-14T20:26:32Z Indexed on 2012/11/16 23:17 UTC
Read the original article Hit count: 260

Filed under:

When learning about polymorphism you commonly see something like this

class Base { int prv_member; virtual void fn(){} }
class Derived : Base { int more_data;  virtual void fn(){} }

What is upcasting or downcasting? Is (Derived*)base_ptr; an upcast or downcast?

I call it upcast because you are going away from the base into something more specific. Other people told me it is a downcast because you are going down a hierarchy into something specific with the top being the root. But other people seem to call it what i call it.

When converting a base ptr to a derived ptr is it called upcasting or downcasting? and if someone can link to an official source or explain why its called that than great.

© Programmers or respective owner

Related posts about polymorphism