2017-07-13 47 views
-1

下面是我在stackoverflow中找到的包裝類。無法理解包裝類構造函數的聲明

class int_ptr_wrapper 
{ 
public: 
    int_ptr_wrapper(int value = 0) : 
    mInt(new int(value)) 
    {} 

    // note! needs copy-constructor and copy-assignment operator! 

    ~int_ptr_wrapper() 
    { 
     delete mInt; 
    } 

private: 
    int* mInt; 
}; 

我無法理解聲明的含義:

int_ptr_wrapper(int value = 0) : 
    mInt(new int(value)) 
    {} 

你能解釋一下這個聲明在細節的意義?

回答

1

構造函數使用initialization list,其中您只需爲mInt變量動態分配內存。

該構造函數是一樣的:

int_ptr_wrapper(int value = 0){ 
mInt = new int(value); 
}