C++ virtual and imaginary functions

The virtual function in C++ mainly implements the mechanism of polymorphism. Polymorphism is simply a matter of using a pointer to the parent type to point to an instance of its subclass, and then calling the member function of the actual subclass from the parent type’s pointer. This technique allows a pointer to a parent class to take “multiple forms”, which is a generic technique. Generics are basically an attempt to implement mutable algorithms using unchanging code. For example, template technology, RTTI technology, virtual function technology, either try to do compile-time resolution or try to do run-time resolution

With the same function, virtual modifier = overwrite, no virtual= hide

1. If there are no data members in the class, the bytes of the instantiated object are 1 byte, and the data members are the sum of the sizes of the data members; If the class has a virtual member function, it will generate a pointer to the virtual function table on instantiation. The object size is 4 bytes. The virtual destructor can generate a pointer to the virtual table, which is 4 bytes in size.Copy the code

If a member function of the parent class is decorated by Virtual, the subclass inherits the virtual function table of the parent class, if the function of the same name of the subclass is also decorated by Virtual.

It is also possible to overwrite the address of the function pointer of the same name in the virtual function table of the parent class into its own pointer address.

Such as:

#include <iostream>
/** * C++ dummy and virtual Pointers */

using namespace std;

class Shape {
public:
    Shape();

    ~Shape();

    double calcArea(a);
};

class Circle : Shape {
public:
    Circle(int r);

    ~Circle();

protected:
    int m_r;
};

Circle::Circle(int r) {
    m_r = r;
}

Circle::~Circle() {

}

Shape::Shape() {

}

Shape::~Shape() {

}

double Shape::calcArea() {

}

int main(a) {
    Shape shape;
    cout << sizeof(shape) << endl;
    Circle circle(100);
    cout << sizeof(circle) << endl; // an int takes 4 bits
    // Different computer bits display different bits
    // A 64-bit computer displays an 8-bit pointer
    return 0;
}
Copy the code

Output result:

1 // If the object has no data, 1 represents a placeholder for an object. 4 // If the object has member data, the size of the member data is displayed.Copy the code

After being decorated by Virtual

#include <iostream>
/** * C++ dummy and virtual Pointers */

using namespace std;

class Shape {
public:
    Shape();

    ~Shape();

    virtual double calcArea(a); The virtual function table is also a pointer type of 4 bits
};

int main(a) {
     
    Shape shape;
    cout << sizeof(shape) << endl;
    Circle circle(100);
    cout << sizeof(circle) << endl;
    return 0;
}
Copy the code

Output result:

// the computer is 64 bits, and the result is not quite the sameCopy the code

Verify that virtual function tables exist:

 Shape shape;
    cout << sizeof(shape) << endl;
    //&shape gets a pointer to shape that is strongly converted to an int
    int *p = (int *) &shape;
    cout< < (unsigned int) (*p) << endl;
    Circle circle(100);
    cout << sizeof(circle) << endl;
    int *q=(int*)&circle;
    cout< < (unsigned int) (*q) << endl;
    q++;// The 64-bit system only advances 4 bits each time, so it increments twice
    q++;
    cout< < (unsigned int) (*q) << endl;
    return 0;
Copy the code

Output result:

8 4206848 // Address 16 4206880 // Address 100 //int valueCopy the code