2013-05-02 32 views
2

我有一個類如何初始化類類型的載體在構造函數中

class effeid 
{ 
public: 
effeid(int a=0,int b=0,int c=0,int d=0):first(a),second(b),third(c),fourh(d){}; 

int first; 
int second; 
int third; 
int fourh; 
}; 

,然後第二類

class AxeEffect { 
public: 

//and I want to initialize the constructor in that way: 

AxeEffect(int=0,string="",int=0,int=0,int=0,int=0,vector<effeid>???); 

如何初始化向量爲0的一部分嗎?

+0

刪除了我的最後一條評論,我誤解了。所以你想讓'AxeEffect'的構造函數以std :: vector爲參數?如果是這樣,你的意思是0? – Ryan 2013-05-02 20:42:01

+2

'vector to 0'是什麼意思?零矢量(int),還是空矢量? – xbonez 2013-05-02 20:42:46

+1

是否以任何方式與此問題相關的第一類? – typ1232 2013-05-02 20:43:58

回答

0

要創建vector<T>的默認參數,您只需構建一個參數,就像其他參數一樣。

class foo 
{ 
public: 
    foo(const vector<int> &v = vector<int>()) : _v(v) {} 
private: 
    vector<int> _v; 
}; 

此外,我不會拿價值vector。請參考以避免不必要的複製。

+0

,對我來說工作正常 – Sonicpath 2013-05-02 20:56:20

+0

對於C++ 11中的默認對象來說,一個非常棒的快捷方式是'const vector &v = {}'(是的,您默認了一個非const引用臨時)。它可能有失效的可能,但我認爲這個班通常會有點奇怪的設計,因爲它不會。 – chris 2013-05-02 21:04:00

+1

最好的方法是按值取值,然後執行'_v(std :: move(v))'。這在左值情況下具有相同數量的副本,在右值情況下具有一個副本。 – 2013-05-02 21:07:23

相關問題