1. What is an initialization list? What is its nature?
  • The initialization list is as follows:
Student(int age, int height) : m_age(age), m_height(height) {}
Copy the code
  • The essence is as follows:
Student(int age , int height) {
 m_age = age;
 m_height = height;
}
Copy the code
2. How do constructors call each other in C++?New Student (10, 20);When does this code allocate memory?
  • To be called in the initializer list
Student():Student(10, 20) {//doSomething}
Copy the code
  • New Student (10, 20);The new function calls malloc inside the new function and returns the address requested by the heap space. The constructor is then called
Does the constructor of a subclass call the constructor of its parent class by default? In what order? How do you make a subclass display that calls the constructor of the specified parent class?
  • By default, subclasses call constructors that have no arguments from their parent class. The constructor of the parent class is called first, and then the constructor of the child class is called
  • Student(int age , int height): People(10) {}
4. Does the destructor of a subclass automatically call the destructor of its parent class? In what order?
  • The destructor of a subclass automatically calls the destructor of its parent class
  • Call the destructor of the subclass first, and then call the destructor of the parent class
5. Why is it safe to use a pointer to a subclass object? It’s not safe the other way around?
  • Because subclasses contain everything about their parent class
  • And subclasses usually have more data than their parents
6. What is override? What is overloading?
  • Override: subclasses override the default implementation of their parent class
  • Overloading: the function name is the same, but the type or number of function arguments is different
7. Does C++ class method support polymorphism by default? What keywords are needed to support polymorphism in C++? If the parent class is declared virtual, do subclasses also need to declare virtual functions?
  • No, that’s outrageous
  • Key words:virtual
  • Subclasses inherit the virtual functions of their parent class, virtual to the end
8. How much memory does the Cat instance occupy? The paper againAnimal *cat = new Cat(); cat->speak();The process of finding a way to speak?
struct Animal {
 int m_age;
 virtual void speak() {
     cout << "Animal:speak" << endl;
 };
 virtual void run() {
     cout << "Animal:run" << endl;
 };
};


struct Cat : Animal {
 int m_life;
 void speak() {
     cout << "Cat:speak" << endl;
 };
 void run() {
     cout << "Cat:run" << endl;
 };
};
Copy the code
  • ifNo virtual functions occupy 4+4= 8 bytes;There are virtual functionsNeed anotherPlus one pointer size memory, for storageMemory address of the virtual table
  • The process of finding virtual function speak:(1) Cat is a pointer to the cat heap object. (2) The first 8 bytes of the cat heap object are the addresses of the virtual table. (3) The virtual table contains the addresses of the virtual functions
Find the process of calling virtual function from assembly analysis?
  • The following code:
Animal *cat1 = new Cat();
cat1->m_age = 15;
cat1->speak();
cat1->run();
Copy the code
  • The corresponding assembly of the above code
// cat1->m_age = 15
0x100002fe0 <+64>:  movq   -0x10(%rbp), %rax
0x100002fe4 <+68>:  movl   $0xf.0x8(%rax)


0x100002feb <+75>:  movq   -0x10(%rbp), %rax // take the first 8 bytes of the cat1 heap object and assign it to rax.
0x100002fef <+79>:  movq   (%rax), %rcx // Take the first 8 bytes of the virtual index and assign it to RCX
0x100002ff2 <+82>:  movq   %rax, %rdi   // Pass the function caller cat1 as an argument to RDI
0x100002ff5 <+85>:  callq  *(%rcx)      // Call the virtual function speak
Copy the code
10. Analyze the process of finding virtual function calls from memory perspective?
  • The following code:
Animal *cat1 = new Cat();
cat1->m_age = 15;
cat1->speak();
cat1->run();
Copy the code
  • Call the call function to get two addresses
`Cat::speak:
->  0x100003160
`Cat::run:
->  0x1000031a0
Copy the code
  • Then analyze the contents of virtual table through memory analysis
// The memory address of cat, the first 8 bytes
40 40 00 00 01 00 00 00To address0x100004040(In theory this should be a virtual table address)// use 0x0100004040 to find 16 bytes (according to theory, the first 8 bytes are Cat::speak address; The last 8 are the addresses of Cat::run.)
60 31 00 00 01 00 00 00To address0x100003160
A0 31 00 00 01 00 00 00To address0x1000031A0
Copy the code
  • Up and down can correspond to up, which can be explained satisfactorily from memory analysis
11. What does virtual function do? Do all instances of Cat share a virtual table?
  • What virtual functions do: make C++ dynamic by deferring the actual address of a function call to runtime
  • Yes, all Cat objects share the same virtual table. (Question: Are Cat objects in the global and stack areas polymorphic?)
12. What do I do if I want to call a member function of a parent class in C++? Simple and crude
void speak() {
Animal::speak();
}
Copy the code
13. When are virtual destructors used?
  • Virtual destructors: Imaginary functions should be declared virtual (virtual destructors) if there is a parent class pointer to a subclass object
  • When deleting the parent pointer, the destructor of the subclass is called to ensure the integrity of the destructor
14. How to define pure virtual functions?
  • A virtual function that has no function body and is initialized to 0Defining interface specifications
  • virtual void speak = 0 ;