- C ++ memory allocation methods can be summarized into three types.
- 1) distribution from the static storage area: it has been allocated when the internal compilation exists, and the program exists throughout the runtime, such as global variables and static variables.
- 2) stack allocation: When a function is executed, all the storage units of local variables in the function can be created on the stack. When the function execution ends, these storage units are automatically released.
- 3) allocate on the stack: This is also called dynamic memory allocation. When the program runs, it uses malloc or new to apply for any amount of memory, this dynamic memory will always exist unless the program is running or the programmer uses free or delete to release the dynamic memory.
- Polymorphism refers to the ability to associate multiple meanings of a function name, that is, the same call method can be mapped to different functions. This kind of activity that calls a function and the appropriate function body is also called binding ). Depending on the binding phase, the binding can be divided into early binding and late binding. Early binding occurs in the compilation phase of the program, static binding occurs in the running stage of the program. It is called dynamic binding ).
- 1) Early binding (early binding): Also known as polymorphism during compilation, indicates that binding occurs during compilation.
1 //: Poly.cpp 2 #include <iostream> 3 4 using namespace std; 5 6 class A { 7 public: 8 A() : x(5) {} 9 virtual void foo() {10 cout << "x = " << x << endl;11 }12 13 int x;14 };15 16 class B : public A {17 public:18 B() : y(10) {}19 virtual void foo() {20 cout << "x = " << x << endl;21 cout << "y = " << y << endl;22 }23 24 int y;25 };26 27 int main() {28 A a;29 B b;30 31 b.x = 1;32 b.y = 2;33 34 a = b;35 a.foo();36 b.foo();37 38 return 0;39 } ///:~
A is a base class, B is a derived class, where A = B has a ing-up relationship. It can be considered that the ing-up is always safe, because it is from more specialized to more general
After the compilation period is complete, objects A and B are allocated a piece of memory space. Of course, this piece of memory space exists in the stack space.
Therefore, during the compilation phase, the compiler has fixed the memory size of objects A and B. Obviously, object B inherited by Class A obtains all public member functions and member variables of Class A. For more specialized object B, when a = B is executed, because the actual stack memory space of B is larger than that of A, the stack space of a cannot accommodate an additional stack space of B (here is the space for storing Y ), the Public member variable X of object B can still be passed to object a because object
There is no member variable named Y, so there is no extra stack space to store the y passed by object B. This is the famous section problem)
- 2) late binding: Also known as dynamic Association, it refers to the implementation of polymorphism at runtime.
1 //: Poly.cpp 2 #include <iostream> 3 4 using namespace std; 5 6 class A { 7 public: 8 A() : x(5) {} 9 virtual void foo() {10 cout << "x = " << x << endl;11 }12 13 int x;14 };15 16 class B : public A {17 public:18 B() : y(10) {}19 virtual void foo() {20 cout << "x = " << x << endl;21 cout << "y = " << y << endl;22 }23 24 int y;25 };26 27 int main() {28 B *b = new B;29 b->x = 1;30 b->y = 2;31 32 // A *a = new B;33 A *a = NULL;34 a = b;35 a->foo();36 b->foo();37 38 return 0;39 } ///:~
Object B is a class pointer. Of course, the pointer is located in the stack space, but the space applied for using new is allocated to the heap space. That is to say, A pointer of type B is stored in the stack space, and the pointer content is the address of the heap space. Object A is a class pointer, And the pointer position is also in the stack space, however, I did not apply for a heap space. That is to say, a type of storage in the stack space is
A's null pointer.
Yes, a is the pointer to the base class A, and B is the pointer to the derived class B. As mentioned earlier, the upward ing is always safe. This time, A = B only changes the pointer to, at this point, the pointer points to the heap space of B. Classes A and B both have the foo function, and virtual functions are modified as virtual functions (This realizes the possibility of Dynamic Association ), when a-> Foo () is executed, the compiler
A finds the vptr pointer, and then finds the corresponding virtual function in vtbl of Class A and Class B, it is found that only the foo of Class B can satisfy the content that a points to the heap space at this time (it is exactly the heap space applied by B, which contains x and y), so a-> Foo () and B-> Foo () finally calls Foo () of Class B ()
- 3) Static and Dynamic Association
There is only one way to implement polymorphism in Java, that is, dynamic association, that is, through pointers or references. from another perspective, the problem caused by static concatenation may be section problem, which is caused by the failure to expand the stack space; dynamic concatenation mainly determines the calling method through the object pointer or reference at runtime (using virtual to modify the function). Therefore, inline is not applicable to modification, but for virtual functions, avoid using it in any case.
Inline, although this can be executed in static Association