[C++] Type conversion [4 types of type conversion]

Table of Contents

1. Type conversion in C language

2. Four type conversions in C++

2.1 static_cast

3.2 reinterpret_cast

3.3 const_cast

3.4 dynamic_cast

3. express

4. RTTI (Understand)


1. Type conversion in C language

exist
C
language, if
The left and right sides of the assignment operator have different types, or the formal parameter types do not match, or the return value type does not match
When the type of the received return value is inconsistent, type conversion needs to occur
,
C
There are two forms of type conversion in the language:
Implicit typing
Conversions and explicit type conversions
.

  • Implicit type conversion: The compiler automatically performs it during the compilation phase. If it can be converted, it will be converted. If it cannot be converted, the compilation will fail.
  • Explicit type conversion: users need to handle it themselves

Defects:

Conversion visibility is poor, all conversion forms are written in the same form, making it difficult to track incorrect conversions

2. Four types of type conversion in C++

C
Style conversion format is simple, but disadvantages:

  • Implicit type conversion may cause problems in some cases: for example, data precision is lost
  • Explicit type conversion mixes everything together and the code is not clear enough

therefore
C++
Proposed its own type conversion style, please note
Because
C++
Compatible
C
Language, so
C++
Also available in
C
Linguistic
Transformation style
.

Standards
C++
To enhance the visibility of type conversions, four named cast operators are introduced:

static_cast, reinterpret_cast, const_cast, dynamic_cast

2.1 static_cast

static_cast
For conversions to non-polymorphic types (static conversions), any type conversion implicitly performed by the compiler is available

static_cast
, but it cannot be used to convert two unrelated types

3.2 reinterpret_cast

reinterpret_cast
Operators typically provide a lower-level reinterpretation of the bit patterns of operands, used to convert a type
of a different type

If reinterpret_cast is changed to static_cast in the code, an error will be reported

3.3 const_cast

const_cast
The most common use is to delete variables
const
Attributes for easy assignment

Frequently asked interview questions:

The observation window is always 20

The output results are 20 and 10

Reason: const type variables are placed in registers by default [Because const variables cannot be changed], registers are faster.
I can’t access the memory directly. I have to put it in the cache or register first, and access it directly in the register, so you
What is changed is what is in the memory, but what is actually fetched is what is in the register. The reason why 10 is printed here is becausethe memory stored in ci has been changed, but ci has been put into the register, and you have to fetch it from the register. It’s still 10, which is essentially caused by the compiler’s optimization mechanism for const object access.

If you want to disable the compiler from optimizing, you can use the keyword volatile

volatile const int ci = 10;//This will be fetched from the memory

3.4 dynamic_cast

dynamic_cast
A pointer to a parent class object
/
Reference converted to a pointer or reference to a subclass object
(
Dynamic conversion
)

Upcast: subclass object pointer
/
Quote
->
Parent class pointer
/
Quote
(
No conversion required, assignment compatible with rules
)

Downcast: parent class object pointer
/
Quote
->
Subclass pointer
/
Quote
(
Use
dynamic_cast
Transformation is safe
)

Note:

1. dynamic_cast
Can only be used for classes whose parent class contains virtual functions

2. dynamic_cast
It will first check whether the conversion can be successful, convert if successful, and return if not
0(nullptr is also OK)

class A
{
public:
virtual void f()
{}
int _a;
};

class B : public A
{
public:
int _b;
};

void f_cast(A* pa)
{
//If you want to distinguish whether pa points to a parent class or a subclass object
//B* pb = (B*)pa; //Forced type conversion in C language cannot solve the problem
//pb->_a = 1;
//pb->_b = 2;
\t
//If pa points to the subclass object, the conversion is successful. If pa points to the parent class object, the conversion fails and nullptr is returned.
B* pb = dynamic_cast<B*>(pa);
if (pb != nullptr)
{
cout << "Conversion successful: pa points to subclass object" << endl;
pb->_a = 1;
pb->_b = 2;
}
else
{
cout << "Conversion failed: pa points to parent class object" << endl;
}
}

int main()
{
    A a;
    B b;
    
    A* pa = &a;
    f_cast(pa);

    pa = &b;
    f_cast(pa);

    return 0;
}

Run results:

How does dynamic_cast identify whether the parent class pointer points to the parent class or the subclass object? [dynamic_cast principle]

Note:

Forced type conversion turns off or suspends normal type checking. Before each use of forced type conversion, you should carefully consider whether there are other different ways to achieve the same purpose. If non-forced type conversion is not possible, then The scope of the cast value should be limited to reduce the chance of errors.
It is strongly recommended to avoid casts

3. explict

Modify single-parameter constructor:

Modify multi-parameter constructor:

4. RTTI (understand)

RTTI: The abbreviation of Run-time Type identification, that is: Run-time type identification.

C++
Support by
RTTI
:

1.
typeid
Operator

2.
dynamic_cast
Operator

3.
decltype