C + + learning and its reverse summary

This article is applicable to students who have a certain foundation of C language, but have zero foundation of C + +, and explains some concepts in C + +

When C defines a structure, it defines a new data type
When C + + defines a structure, it will have a this pointer pointing to the address of the structure. The value of the this pointer is generally given to ecx. The main applications are as follows

typedef struct
{
    int x = 1;
    int y = 2;
    void function(int x, int y)
    {
        this->x = x;
        this->y = y;
    }
}str;
int main()
{
    str p;
    p.function(3,4);
}

In this way, you can modify the variable value in the structure
In C + +, if some struct s are repeated, we won't bother to write again. We can copy them directly. C + + provides a very convenient inheritance

struct person
{
    int age;
    int sex;
};

struct A
{
    int age;
    int sex;
    char name;
}

struct A:person
{
    char name;
}

Both methods are similar. person is the father of A, which is called the parent class. The parent class pointer can be used to access the parent class in the child class.
Moreover, if the subclass and parent class have the same variables defined, you need to add a class name and two colons before the subclass variables
Constructor is a function that can be started directly after declaring a class. It can be overloaded, can have parameters, and has no return value

class test
{

    public:
    test(int x,int y)
    {
        printf("%d\n%d\n",x,y);
    }
};

int main()
{
    test base(2,3);
}
~   

Destructor, a function executed after this class is not used, can be used for free, can not be overloaded, can not have parameters, and has no return value
There are parent classes, subclasses, parent constructs, child constructs, child constructs, and parent constructs
Virtual function table
If virtual is added before a function, it is a virtual function call:

mov ecx,dword ptr [ebp-10]
mov edx,dword ptr [ecx];Virtual table address to edx
mov esi,esp
mov ecx,dword ptr [ebp-10]
call dword ptr [edx];The data in the virtual table address is the virtual table

On the bottom layer, an additional 4 bytes (32 bits) will be stored from the this pointer, which is the address of the virtual table
This virtual table is an array with function addresses
If there is a parent class, the virtual table of the parent class is directly taken and the child class is written. If the virtual function names of the child class and the parent class are repeated, the functions on the virtual table of the parent class from copy in the child class will be overwritten. This is dynamic binding. On this basis, every time a subclass inherits more than one parent class, there will be one more virtual table, that is, 4 bytes (32 bits) more. The virtual functions of the subclass will be placed in the first virtual table, and other virtual tables will be placed in the virtual functions of other parent classes (not recommended)
Therefore, the virtual table is taken:

class base
........
base b;
printf("The address of the virtual table is%d",*(int*)&b);

polymorphic

#include<stdio.h>
class base
{
        public:
                int x;
                int y;
        public:
                base()
                {
                        x = 1;
                        y = 2;
                }
                virtual void print()
                {
                        printf("base:%x %x\n",x,y);
                }
};
class sub1:public base
{
public:
        int A;
        sub1()
        {
                x = 4;
                y = 5;
                A = 6;
        }
        virtual void print()
        {
                printf("sub1:%x %x %x\n",x,y,A);
        }

};
class sub2:public base
{
public:
        int b;
        sub2()
        {
                x = 7;
                y = 8;
                b = 9;
        }
        virtual void print()
        {
                printf("sub2:%x %x %x",x,y,b);
        }

};

void test()
{
        base b;
        sub1 s1;
        sub2 s2;

        base* arr[] = {&b,&s1,&s2};
        for(int i = 0;i<3;i++)
        {
                arr[i]->print();
        }
}
int main()
{
        test();
}

Run results above

base:1 2
base:4 5
base:7 8//virtual is not added in front

base:1 2//Followed by virtual
sub1:4 5 6
sub2:7 8 9

This also indicates that destructors are best defined as virtual functions

We wrote a function that passes parameters like this

func(int* a)
{
    *a = 10;
    //a = (int*)203020;
}

We know that generally speaking, the function cannot change the value of the parameter. If the parameter passed in is ebp+8, and then ebp+8 is given to eax, the value we modify in the function is the value of eax, not the value of the parameter, but let's observe the disassembly above

*a = 10;
mov eax,dword ptr [ebp+8]
mov dword ptr [eax],0ah

It can be seen that if an address is passed in, the data in the address can be changed directly, that is, the parameters can be modified
So there is a reference symbol in C + +

void Test(int &x)
{
    x = 1;
    //x = 2;
}
int main()
{
    Test(a);
}

The parameters are the same at the bottom

lea eax,[ebp-4]
push eax
call@ILT+0(Test)

What's different is that there is a case where the value of the pointer is modified. For example, the code annotated in the first line above is that the pointer is pointing indiscriminately
If you use a reference, you can't point indiscriminately. For example, x = 2. Just give 2 to this variable. The compiler doesn't allow the reference to point to other places. The X in the reference always represents a
For the same data type, the pointer is to point to another, regardless of the original, and the reference is to modify the value of the thing pointed to
In addition, there are too many parameters when passing structures. It is better to directly pass references

We all know that the members and data members in class are private

class Person
{
public:
    Person(int x,int y)
    {
        this->x = x;
        this->y = y;
    }
//friend void print(const Person& refPer);
};
void Print(Person &p)
{
    printf("%d %d\n",p.x,p.y);
}

If so, it can't be typed because it is private. Only other members of the type can access it, such as their own methods, but we have to access the whole (access memory through pointers)
Then we can write the code in the comment and tell the class that this print is its friend. You can access private members by accessing the class through this function

In C + +, we usually say that we want to new an object and delete it when it is not in use. In fact, new encapsulates malloc again, which is one more layer than malloc. For example, int* pi = new int; The space of int size is allocated, so the application object person* pa = new person(1,2); And execute the constructor, that is, the purpose of new an object is to put it in the heap, otherwise we can only put it in the global variable (data) or local variable (stack)

Note that delete and new must be matched if new int[10]; Be sure to delete [];

I will use C + + to do it in the future

Posted on Fri, 03 Dec 2021 17:52:00 -0500 by sissy