Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 462
Moeb Avatar asked Dec 04 '25 21:12

Moeb


2 Answers

Downcasting is the act of casting a reference of a base class to one of its derived classes.

http://en.wikipedia.org/wiki/Downcasting

No, you do not downcast, since double and int are not classes.

like image 125
Andrey Avatar answered Dec 07 '25 15:12

Andrey


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.

like image 41
Daniel Bingham Avatar answered Dec 07 '25 16:12

Daniel Bingham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!