2013-08-17 40 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