2013-08-17 38 views
3

有什麼辦法可以從同一個頭文件中獲得一個全局結構體列表並且初始化包含它們的修改版本的向量嗎? 我知道我不能直接訪問和編輯.h文件中的變量,因爲它不是運行時代碼,但也許碰巧有一個解決方法 - 或者可能是我剛剛跳過的一些非常基本的方法C++初學者手冊..如果是這樣,請原諒我!在頭文件中初始化可定製結構的向量

做出了榜樣,讓我們​​說我有一個領悟了幾個成員的結構,並宣佈在.h文件中一些全局的人..

struct MyStruct 
{ 
    unsigned int a; 
    string foo; 
    float myfloat; 
}; 

MyStruct struct_one={1,"hello",0.238f}; 
MyStruct struct_two={10,"salve",3.14f}; 
MyStruct struct_three={3,"bonjour",0.001f}; 
MyStruct struct_four={6,"dias",5.0f}; 

然後我就可以初始化它們含有這樣的載體(不知道這是最好的一個,雖然)方式

MyStruct MyStructArray[] = {struct_one, struct_two, struct_three, struct_four}; 

vector<MyStruct> MyStructVector(MyStructArray, 
MyStructArray+sizeof(MyStructArray)/sizeof(MyStructArray[0])); 

但是我想能夠改變,在飛行中,一些結構的成員,而無需創建矢量(或陣列)之前改變全球。 可能嗎?

編輯:「在一個頭文件」我的意思是,「在一個頭文件」。如果我在標頭中做unsigned int a = 100;,我不必在實際源中初始化或調用某些東西來使其工作。這個向量本身完美地工作,我只想知道是否有一種方法可以從原始全局結構的修改版本中構建它..也就是說,我想使用相同的全局結構,但對於成員a具有不同的值。

+1

。想想如果將頭文件包含在多個源文件中會發生什麼,然後您有多個相同變量的定義。 –

+1

如果全局變量位於頭文件中,那麼它們最好是'extern',並且在* single * .cpp文件中進行初始化。如果你使用兼容的C++ 11實現,可以利用['std :: begin()'](http://en.cppreference.com/w/cpp/iterator/begin)和['的std ::端()'](http://en.cppreference.com/w/cpp/iterator/end)。你的'MyStructArray'將有原始結構的副本;不參考他們。因此,如果這是您的擔憂,那麼更改'MyStructArray'不會更改'struct_one'等。 – WhozCraig

+0

那麼,現在我只有一個.cpp文件,所以我還沒有遇到這種問題呢.. 但是,我該如何使用'std :: begin()'和'std :: end( )'?我對他們很陌生,我必須說.. – Banderi

回答

2

在@Sam的回答之上......

使用構造函數:

struct MyStruct { 
    unsigned int a; 
    string foo; 
    float myfloat; 

    MyStruct(unsigned int a, const string& foo, float myfloat) : a(a) , foo(foo), myfloat(myfloat) {} 
}; 

現在你可以用一個簡單的語句

vec.push_back(MyStruct(1, "hello", 0.238f)); 
你的結構增加了矢量
class Foo { 
public: 
    static std::vector<int> MyStructVector; 
} 

inline std::vector<MyStruct> MakeVector() 
{ 
    std::vector vec; 
    vec.push_back(MyStruct(1, "hello", 0.238f)); 
    //... 
    return vec; 
} 

std::vector<MyStruct> Foo::MyStructVector= MakeVector(); 
+0

Sam的回答顯然已經消失了,但它並沒有工作,因爲我仍然需要調用函數來完成.cpp文件中的工作,這正是我想要避免的。(希望迴應線程也不遲) – Banderi

+0

@Banderi只要您使用關鍵詞inline來停止多重定義錯誤,您就可以在頭文件中添加函數 – andre

+0

但是它不起作用..如果我運行代碼,就像函數中沒有發生過任何事情一樣。 「內嵌」之後,我還需要做些什麼嗎? – Banderi

0

我不知道如果我得到你的問題正確的:如果你把變量定義在頭文件,那就不要(我是愚蠢的名字)

#include <vector> 
#include <iostream> 

// Header 
// ====== 

struct MyStruct 
{ 
    unsigned int a; 
    std::string foo; 
    float myfloat; 

    MyStruct(unsigned a, const std::string& foo, float myfloat) 
    : a(a), foo(foo), myfloat(myfloat) 
    {} 
}; 

typedef std::vector<MyStruct> MyStructs; 

extern MyStructs& get_structs(); 

struct AppendToMyGlobalMyStruct { 
    AppendToMyGlobalMyStruct(unsigned a, const std::string& foo, float myfloat) { 
     get_structs().push_back(MyStruct(a, foo, myfloat)); 
    } 
}; 


// Source 
// ====== 

// Have initialization done at first function call, only: 
MyStructs& get_structs() { 
    static MyStructs s; 
    return s; 
} 

// Another source 
AppendToMyGlobalMyStruct a(0, "0", 0); 
// Another source 
AppendToMyGlobalMyStruct b(1, "1", 1); 
// Another source 
AppendToMyGlobalMyStruct c(2, "2", 2); 

int main() 
{ 
    MyStructs& s = get_structs(); 
    std::cout << s[0].foo << std::endl; 
    std::cout << s[1].foo << std::endl; 
    std::cout << s[2].foo << std::endl; 
    return 0; 
} 

+0

嗯..不,我想保留頭文件中的所有工作,並且我已經可以輕鬆地從那裏訪問全局的東西,而無需在任何地方調用它。我只需要構建一個基於原始結構的矢量,但不一樣。 – Banderi