我正在努力與我的一個類的構造函數做一個成員 未正確初始化。在構造函數中初始化向量 - C++
我有一個「設置」類來處理我用於模擬 的設置以及執行模擬步驟的類Simulations。
我不能理解的是,爲什麼像想象的那樣代碼不起作用:
class Settings{
public:
int n ; // a number I need to create properly a vector in my class simulation
// ... rest of the code constructors etc to read values from files.
// everything works fine and the values are assigned properly
}
class Simulation{
public:
std::vector<int> v ;
Settings *SP;
Simulation(Settings *);
}
Simulation::Simulation(Settings *pS)
:SP(pS), v(std::vector<int>(SP->n,0)) {} // the constructor doesn't work,
// v is initialized but it is not created as a vector of size n, but 0.
我認爲這是在我使用構造的方式有問題,但我不明白爲什麼。
順便說一句,在大括號內定義v的工作很好,我只是好奇地想知道爲什麼 定義它正確的方式不能按預期工作!
非常感謝您的幫助!
請說明問題的簡短,完整,可編譯的示例。 – 2012-08-06 03:02:38