Convenient C++ struct initialisation

后端 未结 13 773
谎友^
谎友^ 2020-12-07 10:15

I\'m trying to find a convenient way to initialise \'pod\' C++ structs. Now, consider the following struct:

struct FooBar {
  int foo;
  float bar;
};
// jus         


        
13条回答
  •  星月不相逢
    2020-12-07 10:39

    Option D:

    FooBar FooBarMake(int foo, float bar)

    Legal C, legal C++. Easily optimizable for PODs. Of course there are no named arguments, but this is like all C++. If you want named arguments, Objective C should be better choice.

    Option E:

    FooBar fb;
    memset(&fb, 0, sizeof(FooBar));
    fb.foo = 4;
    fb.bar = 15.5f;
    

    Legal C, legal C++. Named arguments.

提交回复
热议问题