SYSU programming C++ (12th week) derived type compatibility, object type conversion, multiple inheritance, virtual base class

tips:

        1. Do not write delete in the copy constructor, only write in operator=

        2. When writing copy construction, the inherited base class should also call the construction in the: list.

Derived Type Compatibility

1. Objects of derived classes can be assigned to base class objects, and vice versa

2. You can assign a public derived class object to a base class pointer, and vice versa (this point must be a public derived class to be compatible)

That is, the base class can be assigned by the derived class , the base class is compatible with the derived class, and the derived class is not compatible with the base class

object type conversion

Upcasting (is implicit) :

        Convert derived class objects into base class objects so that the base class can be assigned by the derived class

Downcasting (to be explicit) :

        Convert base class objects to derived class objects. In this way, the derived class can be assigned by the base class (because the base class is explicitly converted into a derived class)

        dynamic_cast <> has the function of dynamic type checking and is safer than static_cast)

C++ mandatory type conversion

1. Static_cast<>()  Disadvantage: There is no type checking at runtime to ensure the safety of conversion

2. dynamic_cast<>()    dynamic type conversion, check type safety at runtime (return NULL if conversion fails)

3. reinterpret_cast<>() is  generally recommended for converting pointers

        • For example, C language void * can implicitly transform any type of pointer; C++ void * needs to explicitly transform other types of pointers

                char *cp; void *gp; cp=reinterpret_cast<char*>(gp) //so explicit conversion

4. const_cast<> () removes the constness of constant pointers/references

Features of const constants (constant variables):

• Feature 1: It is a variable. The value of the constant variable can be changed through the pointer pointing to this constant variable.

        Therefore, const_cast can be used to change the pointer of the constant variable into a modifiable pointer, thereby modifying the value of the constant variable

• Feature 2: It is a constant. Use the name of this constant variable directly, it is still the original constant value, it will not be changed when the pointer is changed.


 

multiple inheritance

Allowed:

Not allowed:

 

That is, direct base classes cannot contain each other  during multiple inheritance .

Both declarations and constructs are comma-connected compared to single inheritance :

virtual base class

Add the reserved word "virtual" before         inheriting access control , then the base class is a virtual base class

        The difference between a virtual base class and a normal base class is only manifested when the derived class repeatedly inherits from the base class

        The virtual base class has only one copy in the derived class , which will eliminate repeated inheritance

The order in which constructors are called when creating derived class objects:

        ① First call the constructor of the virtual base class;

        ② Second, call the constructor of the common base class, multiple base classes are from left to right in the order listed when the derived class is declared, not in the order in the initialization list;

        ③ Call the constructor of the object member again, and call it in the order in which the object members appear in the class declaration, not in the order in the initialization list

        ④ Finally, the constructor of the derived class is executed.

For example, the red line represents the virtual base class, and the black line represents the common base class, then the sequence of invocations to construct derived is as follows:

Guess you like

Origin blog.csdn.net/jz_terry/article/details/130621164