2011-06-10 83 views
0

,它具有以下行一個C++的語法問題:類下面的代碼方法

base_list(const base_list &tmp) :memory::SqlAlloc() 

base_list是一種方法,記憶是一個命名空間,SqlAlloc是一類,所以這是什麼意思結合起來時,他們在一起?

class base_list :public memory::SqlAlloc 
{ 
public: 
    base_list(const base_list &tmp) :memory::SqlAlloc() 
    { 
    elements= tmp.elements; 
    first= tmp.first; 
    last= elements ? tmp.last : &first; 
    } 

回答

3
base_list(const base_list &tmp) :memory::SqlAlloc() 

用途Initializer list調用SqlAlloc類的構造函數中的命名空間memory

有關在C++中使用Initializer List的優點的更多信息,請參閱this

2

它調用基類memory::SqlAlloc()的默認構造函數。

namespace memory { 

class SqlAlloc 
{ 
public: 
    SqlAlloc() {} // SqlAlloc's default constructor 
}; 

} 

//... 

class base_list : public memory::SqlAlloc 
{ 
public: 
    // base_list constructor 
    base_list(const base_list &tmp) : memory::SqlAlloc() 
    { 
    // The code after the ":" above and before the "{" brace 
    // is the initializer list 
    elements= tmp.elements; 
    first= tmp.first; 
    last= elements ? tmp.last : &first; 
    }; 

考慮以下幾點:

int main() 
{ 
    base_list bl; // instance of base_list called "bl" is declared. 
} 

當創建bl,它調用的base_list構造。這會導致base_list構造函數的初始化程序列表中的代碼運行。該初始化程序列表有memory::SqlAlloc(),它調用SqlAlloc的默認構造函數。當SqlAlloc的構造函數完成時,則運行base_list的構造函數。

1

base_list是構造函數,它調用基類(SqlAlloc)的構造函數。

0

base_list繼承。

您詢問的行是複製構造函數。之後的: memory::SqlAlloc()是基類初始值設定項。它調用基類的構造函數。