Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
3  
Related: "Who decided on the terminology downcasting and upcasting?" Actually ... duplicate? –  Steven Jeuris May 14 '12 at 20:32
3  
It's not a duplicate. This question asks what it is, the other one asks who came up with the names. –  Robert Harvey May 14 '12 at 20:44
1  
@RobertHarvey "and if someone can link to an official source or explain why its called that than great.", but agreed, that could be considered as only part of the question. –  Steven Jeuris May 14 '12 at 20:49
    
watch out for that virtual in the Derived class. You do not want to write that unless you derive the Derived class one more time. void fn(){...} is enough. –  appoll May 14 '12 at 21:33
    
JustaPro: adding virtual is unnecessary but doesn't "hurt". Can you elaborate on why you think it should not be used at all in that context? –  Nik Bougalis Nov 16 '12 at 23:41

3 Answers 3

up vote 9 down vote accepted
Is (Derived*)base_ptr; an upcast or downcast?

It is a downcast: see Wikipedia's article on it.

In object-oriented programming, downcasting or type refinement is the act of casting a reference of a base class to one of its derived classes.

In many programming languages, it is possible to check through RTTI whether the type of the referenced object is indeed the one being cast to or a derived type of it, and thus issue an error if it is not the case.

In other words, when a variable of the base class (parent class) has a value of the derived class (child class), downcasting is possible...

share|improve this answer

Think of the normal way of drawing a tree with the "base" at the top of the page and the leaves at the bottom. If you represent your class hierarchy this way, with the base class at the top and the leaves somewhere below, then up-casting and down-casting become literal -- something that moves up the page from a leaf toward the base is an up-cast and something that moves down the page from the base toward a leaf is a down-cast.

share|improve this answer
    
But trees go upwards and their trunks are on the ground coming out of soil. (This is why i keep thinking upcast) –  acidzombie24 May 14 '12 at 21:05
    
@acidzombie24: But in books on computer science, you'll find that trees are normally drawn as I've described, with the root at the top. –  Jerry Coffin May 14 '12 at 21:10
    
I know but i was just being funny. Trees(real ones) and base (like foundation and under my feet) are such bad ways to think of it :(. +1 –  acidzombie24 May 14 '12 at 21:15
3  
@acidzombie24: Just think of them as trees in Australia. –  Loki Astari May 14 '12 at 23:29

In most languages (as far as I know) generalizing casts are performed automatically. So, usually if you speak about a cast within a hierarchy you are referring to a specializing cast. IMHO most people with the desire to separate the two types call the specializing cast downcast because they think of the inheritance hierarchy as a tree. And in computer science the root is pictured to be on the top. The reason behind this is either a convention, or, more probably, a deep rooted hatred for biologists among computer scientists. (All puns intended.)

EDIT: Maybe it is a top rooted hatred.

share|improve this answer
    
LOL must +1 :). –  acidzombie24 May 14 '12 at 21:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.