SoFunction
Updated on 2025-03-11

Do you know C++ type conversion (strong conversion)

Static type conversion

Keywords:static_cast

1,Allow conversion between built-in data types

    char a = 'a';
	double b = static_cast<double>(a);
	cout << b << endl;//97
	return 0;

<> is the data type after being transferred. The name of the variable to be transferred in ().

2,Allows conversion of pointers or references between subclasses and parent classes

Up-type conversion is safe. (Son turns into father)

Down-type conversion is not safe. (Father's Rotor)

Pointer conversion:

class Animal
{
};
class Dog :public Animal 
{
};
class Other
{
};
int main()
{
	Animal* animal = NULL;
	Dog* dog = NULL;
	//Convert animal to Dog* (down type conversion, unsafe)	Dog * dog2 = static_cast&lt;Dog*&gt;(animal);
	Other* other = static_cast&lt;Other*&gt;(animal);//A mistake will occur at this time, because the two pointers are not father-son relationships.	return 0;
}

Therefore, pointers to parent and child classes can be converted by static conversion.

Quote Conversion:

Animal animal;
Dog dog;
Animal& animalCopy = animal;
Dog& dogCopy = static_cast<Dog&>(animalCopy);
Dog& dogCopy2 = dog;
Animal& animalCopy2 = static_cast<Animal&>(dogCopy2);

After the object is created, a reference to the object is created, and the created reference can then be converted to the reference type of the child/parent class.

Dynamic type conversion

Almost the same as static type conversion, it is safer than static type conversion. (Because it has the function of type checking when performing down-type conversion; if accuracy or information is lost, conversion is not allowed)

Keywords:dynamic_cast

1. Conversion between built-in data types is not allowed

2. Between the parent class and the child class

(1) Allow conversion of pointers or references between parents and children.

(2) Sometimes there will be problems with the father's rotor. If polymorphism occurs, then the conversion is always safe.Polymorphism means using the pointer of the parent class to point to the object of the subclass, and there are virtual functions in the parent class. The subclass overrides the virtual functions in the parent class.

Constant conversion

Keywords:const_cast

This operator is used to modify the const attribute of the type (if it was originally a constant, it becomes a non-const after use; if it was originally a non-const, it becomes a constant after use)

(1) The constant pointer is converted into a non-const pointer and points to the original object.

(2) Constant reference is converted into a very constant reference and points to the original object.

Note: This keyword is only for pointers and references and cannot be modified for other const attributes.

	//pointer:	const int* p = NULL;
	int* pp = const_cast&lt;int*&gt;(p);
	const int* ppp = const_cast&lt;const int*&gt;(pp);
	//Quote:	int num = 10;
	int&amp; num2 = num;
	const int&amp; num3 = const_cast&lt;const int&amp;&gt;(num2);

Reinterpret the conversion

mostInsecureconversion. (The int type can be converted to int* type)

Keywords:reinterpret_cast<int*>(a)

	int a = 10;
	int* p = reinterpret_cast<int*>(a);

Pointers can also be converted between different classes (unrelated classes).

Summarize

That’s all for this article. I hope it can help you, and I hope you can pay more attention to more of my content!