In C++ is slicing and casting the same? Or are there some differences? I am studying for a C++ and Object-Oriented Design exam and in the course notes I am reading they use both words but I do not know if it is exactly the same.
Asked
Active
Viewed 43 times
0
-
2"*is slicing and casting the same*" - no. See [What is object slicing?](https://stackoverflow.com/questions/274626/) – Remy Lebeau May 17 '22 at 18:01
-
1Different things: Casting means use an object as though it has a different type. Slicing means a derived class is copied to an instance of the base class, "slicing" off all of the value added by the derived class. – user4581301 May 17 '22 at 18:01
-
Casting is going from `T` to `U`. Slicing is going from `T::U` to `T` – NathanOliver May 17 '22 at 18:07
-
"Casting" is an explicit conversion. `float(1)` is a cast, but no slicing is involved, and no classes are involved. "Slicing" is copying (or moving) a base class subobject from a derived class object. It can be done with a cast, but not necessarily, e.g. `Derived a; Base b = a;`. – HolyBlackCat May 17 '22 at 18:10