Lesson 55 classic problem resolved four

1. With regard to dynamic memory allocation

new and malloc What is the difference?
delete and free What is the difference?

new keyword malloc difference function:

A.
 new new keywords are part of c ++
 malloc is a function provided by the c library

B.
 new new type in particular units of memory allocation
 malloc bytes for memory allocation

C.
new new in the application memory space may be initialized (temporary object)
the malloc need to apply only the needed amount of memory space

d.
new new constructor call can trigger
malloc memory allocation only needed

e.
 to create an object can only choose new, you can not select malloc (not trigger constructor)

The difference between delete and free of

A.
 the Delete key is part of the c ++ is
 free is a function provided by the c library

B.
Delete capable of triggering call the destructor of
memory space previously allocated only free return

c.
 the destruction of the object can only use delete, you can not select free

eg:

#include <iostream>
#include <cstdlib>

using namespace std;

class Test
{
public:
    Test()
    {
        cout << "Test::Test()" << endl;
    }
    
    ~Test()
    {   
        cout << "~Test::Test()" << endl;
    }
};

int main()
{
    Test* pn = new Test;        // 分配内存空间(new Test),同时初始化这段内存
                                // 空间Test对象(临时对象)
                                // new能触发构造函数
    Test* pm = (Test*)malloc(sizeof(Test));     //仅仅分配一段空间
    
    delete pn;        // delete能够触发析构函数
    free(pm);
    
    return 0;
}

2. With regard to virtual functions

Whether constructor can become a virtual function?
Are destructor can become a virtual function?

answer:

a. Constructors can not be virtual function
 during the execution of the constructor, virtual function tables are not initialized properly. Virtual functions defined at this time, using the virtual function table, inevitable errors.
 At the end of the constructor is executed, the virtual function table pointer will be initialized correctly

b. destructor can be virtual function
 destructor is called before the object is destroyed, then the virtual function pointer is pointing to the correct virtual function table
 is recommended in the design of class destructor declared virtual

eg:

#include <iostream>
#include <string>

using namespace std;

class Base
{
public:
    Base()
    {
        cout << "Base()" << endl;
        
        func();
    }
    
    virtual void func() 
    {
        cout << "Base::func()" << endl;
    }
    
    //virtual ~Base()
    ~Base ()        
    {
        func();
        
        cout << "~Base()" << endl;
    }
};


class Derived : public Base
{
public:
    Derived()
    {
        cout << "Derived()" << endl;
        
        func();
    }
    
    virtual void func()
    {
        cout << "Derived::func()" << endl;
    }
    
    ~Derived()
    {
        func();
        
        cout << "~Derived()" << endl;
    }
};


int main()
{
    Base* p = new Derived();
    
    // ...
    
    delete p;           // 当析构函数未被定义为 virtual 时 delete p根据指针到类型来调用析构函数
                        // 此时只会调用父类的析构函数。而不会调用子类的析构函数。错误
                        // 当使用虚函数时,构成多态。此时编译器会根据p所指的实际对象来决定析构的调用
    
    return 0; 
}

3.

It can occur constructor polymorphism?

Polymorphism may occur if the destructor?

answer:

a. constructor polymorphic behavior can not occur
 when the constructor executes, the virtual function table pointer is initialized properly

B. destructor polymorphic behavior can not occur
 when the destructor executed, virtual function table pointer has been destroyed
(Note here that the destructor can be defined as virtual functions. is because virtual function is called, the virtual function table pointer, or a sound.
However, when the destructor is executed, the virtual function table pointer is destroyed, then the virtual function can not be used to implement a multi-state)

NOTE: constructors and destructors in polymorphic behavior can not occur, only the current version of the function calls defined in the class

Guess you like

Origin www.cnblogs.com/huangdengtao/p/11996561.html