Search code examples
c++conventionsdowncast

Is this not downcasting?


If I do

double d = 34.56;
int i = (int)d;

Am I not "downcasting"?

OR

Is this term only used in terms of classes and objects?


I am confused because in this case we are "downcasting" from a bigger double to a smaller int, but in case of classes, we "downcast" from a smaller base class to a bigger derived class.

Aren't these two conventions, in some sense, opposite?


Solution

  • No, you are not down casting. You are just casting, and you're chopping off anything after the decimal.

    Down casting doesn't apply here. The primitives int and double are not objects in C++ and are not related to each other in the way two objects in a class hierarchy are. They are separate and primitive entities.

    Down casting refers to the act of casting one object into another object that derives from it. It refers to the act of moving down from the root of the class hierarchy. It has nothing to do with the sizes of types in question.