简体   繁体   English

在C ++代码中初始化C结构

[英]Initialising C structures in C++ code

Is there a better way to initialise C structures in C++ code? 有没有更好的方法来初始化C ++代码中的C结构?

I can use initialiser lists at the variable declaration point; 我可以在变量声明点使用初始化列表; however, this isn't that useful if all arguments are not known at compile time, or if I'm not declaring a local/global instance, eg: 但是,如果在编译时不知道所有参数,或者如果我没有声明本地/全局实例,那么这没有用,例如:

Legacy C code which declares the struct, and also has API's using it 遗留C代码,它声明了结构,并且还使用了API

typedef struct
{
    int x, y, z;
} MyStruct;

C++ code using the C library 使用C库的C ++代码

void doSomething(std::vector<MyStruct> &items)
{
    items.push_back(MyStruct(5,rand()%100,items.size()));//doesn't work because there is no such constructor
    items.push_back({5,rand()%100,items.size()});//not allowed either

    //works, but much more to write...
    MyStruct v;
    v.x = 5;
    v.y = rand()%100;
    v.z = items.size();
    items.push_back(v);
}

Creating local instances and then setting each member one at a time ( myStruct.x = 5; etc) is a real pain, and somewhat hard to read when trying to add say 20 different items to the container... 创建本地实例然后一次设置一个成员( myStruct.x = 5;等)是一个真正的痛苦,当尝试将20个不同的项目添加到容器时有点难以阅读...

You're looking for C99 compound literals. 你正在寻找C99复合文字。 Example code: 示例代码:

struct foo *foo = malloc(sizeof *foo);
*foo = (struct foo){ bar, baz };

If you can't add a constructor (which is the best solution in C++03 but you probably have compatibility constraint with C), you can write a function with the same effect: 如果你不能添加构造函数(这是C ++ 03中的最佳解决方案,但你可能与C有兼容性约束),你可以编写一个具有相同效果的函数:

MyStruct makeAMyStruct(int x, int y, int z)
{
    MyStruct result = { x, y, z };
    return result;
}

items.push_back(makeAMyStruct(5,rand()%100,items.size()));

Edit: I'd have checked now that C++0X offers something for this precise problem: 编辑:我现在已经检查过C ++ 0X为这个精确的问题提供了一些东西:

items.push_back(MyStruct{5,rand()%100,items.size()});

which is available in g++ 4.4. 这是以g ++ 4.4提供的。

How about: 怎么样:

MyStruct v = {5, rand()%100, items.size()};
items.push_back(v);

Not clear what you are asking. 不清楚你在问什么。 In C++, the obvious solution is to give the struct a constructor: 在C ++中,显而易见的解决方案是为结构提供构造函数:

struct MyStruct {
  int x, y, z;
  MyStruct( int ax, int ay, int az ) : x( ax ), y( ay ), z( az ) {}
};

创建一个函数来初始化它,类似于C ++构造函数的作用。

Another option is to derive from the struct and add a constructor there. 另一种选择是从struct派生并在那里添加构造函数。

struct MyDerivedStruct : public MyStruct
{
    MyDerivedStruct(int xi, int yi, int zi)
    {
        x = xi;
        y = yi;
        z = zi;
    }
}

Then you can use this derived type in your own code and pass it to the C library when necessary. 然后,您可以在自己的代码中使用此派生类型,并在必要时将其传递给C库。 The language should take care of implicitly converting to MyStruct when appropriate. 在适当的时候,语言应该隐式转换为MyStruct

As a bonus, you could also add other useful member functions, perhaps even wrapping many of the legacy C functions that use this type. 作为奖励,您还可以添加其他有用的成员函数,甚至可能包含许多使用此类型的旧C函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM