C++11---casting
CASTING
In c/cpp system, when i talking about casting, i mean ‘type casting’.
Since c/cpp being a strongly type language, basically means it has a type system. If you specify a variable as int, you cannot treat it as double or float, for example, and vice versa.
which means if we want to covert a variable type - for c/cpp is the underlying language - we need casting system to explictly or implictly.
At the very beginning, we must bear something in our mind that cpp-style casting is not better that c-style casting. On the contrary, it will slow down your code for doing lot of extra check. cpp-style casting is more like a semantic sugar in the language grammer, whereas it cannot be more powerful than c-style. Essentially, they are doing the same work.
implicit cast
int value = 5;
double dou = value;
or
double value = 5.5;
int dou = value;
C-style casting
double dou = 5.5;
int value = (int)dou;
This style of explicit cast with declaring the after coverting type within the (…) is technically called C-style cast.
CPP-style cast
- static_cast: statically cast…(speechless)
to implement the same effect as has declared in the c-style part, the cpp11 style should be implemented like this:
double dou = 5.5;
int value = static_cast<int>(value); // value = 5
- const_cast: use to add / remove ‘const’ of a variable
- remove constance
const int constantValue = 42;
// 使用const_cast删除const属性
int& nonConstRef = const_cast<int&>(constantValue);
nonConstRef = 50;
std::cout << "constantValue: " << constantValue << std::endl; // 输出 "constantValue: 50"
- add constance
int mutableValue = 10;
// 使用const_cast添加const属性
const int& constRef = const_cast<const int&>(mutableValue);
// 尝试修改constRef的值将导致编译错误
// constRef = 20; // 错误:assignment of read-only reference 'constRef'
- reinterpret_cast: to reinterpret a bar of memory in a form that is different from its original form. Usually, it reinterprets a type pointer or reference to another type pointer or reference.
int p = 'a';
int* x = &p;
char* ch = reinterpret_cast<char *>(x);
std::cout << *ch << std::endl;
- dynamic_cast: used primarily for performing type conversions and checking their safety at runtime, especially in the context of polymorphic classes (usually implemented through inheritance and virtual functions).
class Base {
public:
virtual void print() {
std::cout << "Base" << std::endl;
}
};
class Derived : public Base {
public:
void print() override {
std::cout << "Derived" << std::endl;
}
};
int main()
{
Base* basePtr = new Derived();
Derived* derivedPtr = dynamic_cast<Derived*>(basePtr);
if (derivedPtr) // conversion is successful
{
// Conversion successful
std::cout << "Successfully converted to Derived." << std::endl;
derivedPtr->print(); // This will call the print function of the Derived class
}
else // failure
{
// Conversion failed
std::cout << "Failed to convert to Derived." << std::endl;
}
}
So, regarding the above code block, we can find one interesting fact: dynamic_cast is often used to verify which derived class a base class pointer actually points to. Remember this, it is so important!