1. What are the two types of type conversions in C? What are the four types of conversions in C++?
  • C:(type) expression, type (expression)
  • C + + :Static_cast, dynamic_cast, reinterpret_cast, const_cast
  • C++ use format: xxx_cast(expression)
  • The nature of type conversions (carefully) :Casts mostly trick the compiler into making the code compile
2. const_cast
  • Generally used to remove const attributes and convert const to nonconst
const Person *p1 = new Person();
cout << p1->m_age << endl; / / 0
//p1->m_age = 10; / / complains

//Person *p2 = (Person *)p1;
Person *p2 = const_cast<Person *>(p1);
0x100003f6c <+60>: movq -0x10(% RBP), % RDX 0x100003f70 <+64>: movq %rdx, -0x18(%rbp) */
p2->m_age = 20;

cout << p1->m_age << endl;/ / output 20
cout << p2->m_age << endl;/ / output 20
Copy the code
3. dynamic_cast
  • Generally used for polymorphic type conversions, with runtime security checks
  • Run a parent pointer to a child instance. Do not allow a child pointer to a parent instance
Person *p1 = new Person();
Person *p2 = new Student();


cout << "p1 - " << p1 << endl; // p1 - 0x1007660e0
cout << "p2 - " << p2 << endl; // p2 - 0x100764a90

Student *stu1 = dynamic_cast<Student*>(p1);
Student *stu2 = dynamic_cast<Student*>(p2);

cout << "stu1 - " << stu1 << endl; // stu1 - 0x0
cout << "stu2 - " << stu2 << endl; // stu2 - 0x100764a90
Copy the code
4. Static_cast
  • Lack of runtime security checks compared to Dynamic_cast
  • Cannot cross convert (cannot convert if not in the same inheritance system)
  • For basic data type conversions, nonconst to const
5. reinterpret_cast
  • It is a relatively low-level cast, without any type checking or format conversion, justSimple copy of binary data
  • You can cross convert
  • Pointers and integers can be converted to and from each other
Person *p1 = new Person();
Person *p2 = new Student();


cout << "p1 - " << p1 << endl; // p1 - 0x1007660e0
cout << "p2 - " << p2 << endl; // p2 - 0x100764a90

Student *stu1 = reinterpret_cast<Student*>(p1);
Student *stu2 = reinterpret_cast<Student*>(p2);
stu1->m_age = 20;
stu2->m_age = 21;

cout << "stu1 - " << stu1 << " m_age - " << stu1->m_age << endl; // stu1 - 0x100593370 m_age - 20
cout << "stu2 - " << stu2 << " m_age - " << stu2->m_age << endl; // stu2 - 0x100591b60 m_age - 21


int i = 10;
double d1 = reinterpret_cast<double &>(i);
cout << "i - " << &i << " d1 - " << &d1 << endl; // i - 0x7ffeefbff414 d1 - 0x7ffeefbff408
cout << "d1 - " << d1 << endl; // d1 - 5.23561e-307
Copy the code
6. What are the three data segments for storing floating point numbers in a computer?
  • From left to right: sign bit, exponent bit, mantissa
  • 5.23561 e-307Classic floating-point representation
7. What is the function of the auto keyword? Does auto affect the efficiency of the program?
  • Auto can infer the type of a variable from the initialization expression, greatly simplifying programming
  • Auto is a compiler feature (which may affect compilation efficiency); Does not affect the final machine code quality, does not affect the operation efficiency
8. Nullptr has special effect compared to NULL.
  • Nullptr is specifically used to represent null Pointers, resolves the ambiguity of NULL, which can represent any data that is empty
9. How about C++ lambda expressions? What is the essence?
  • Lambda expressions in C++ are essentiallyfunction

10. Some classical ways to write lambda expressions?
int main(a) {
    
    // 1. A lambda expression called directly after the definition
    ([] {
        cout << "lambda1()-------" << endl; }) ();// 2. Because lambda is a function, it can be pointed to with a pointer
    void (*p2)() = [] {
        cout << "lambda2()-------" << endl;
    };
    p2();
    
    // 3. Lambda with arguments and return values
    int (*sum)(int.int) = [] (int a, int b) -> int {
        return a + b;
    };
    cout<< sum(1.2) < <endl; 3 / / output
    
    // 4. Value capture (specify capture) lambda
    int a = 10;
    int b = 0;
    auto p4 = [a]() {
        cout << "lambda4()-------" << a << endl;
    };
    a = 14;
    p4(); Lambda4 ()-------10
    
    // 5. Value capture (all used to capture) lambda
    auto p5 = [=]() {
        cout << "lambda4()-------" << a << " b: " << b << endl;
    };
    a = 15;
    p5(); // lambda4()-------14 b: 0
    
    // 6. Reference to capture (specify capture) lambda
    auto p6 = [&a]() {
        cout << "lambda4()-------" << a << endl;
    };
    a = 16;
    p6(); // lambda4()-------16
    
    // 7. Reference to capture (all used) lambda
    auto p7 = [&]() {
        cout << "lambda4()-------" << a << " b: " << b << endl;
    };
    a = 17;
    b = 27;
    p7(); // lambda4()-------17 b: 27
    
}
Copy the code