简体   繁体   English

用C ++初始化C结构

[英]Initialise C-structs in C++

I am creating a bunch of C structs so i can encapsulate data to be passed over a dll c interface. 我正在创建一堆C结构,所以我可以封装要通过dll c接口传递的数据。 The structs have many members, and I want them to have defaults, so that they can be created with only a few members specified. 结构体有很多成员,我希望它们具有默认值,这样它们只需指定几个成员即可创建。

As I understand it, the structs need to remain c-style, so can't contain constructors. 据我了解,结构需要保持c风格,因此不能包含构造函数。 Whats the best way to create them? 什么是创建它们的最佳方式? I was thinking a factory? 我在想一家工厂?

struct Foo {
    static Foo make_default ();
};

A factory is overkill. 一家工厂太过分了。 You use it when you want to create instances of a given interface, but the runtime type of the implementation isn't statically known at the site of creation. 您希望创建给定接口的实例时使用它,但在创建的站点上不能静态地知道实现的运行时类型。

The C-Structs can still have member functions. C-Structs仍然可以具有成员函数。 Problems will, however, arise if you start using virtual functions as this necessitates a virtual table somewhere in the struct's memory. 但是,如果您开始使用虚函数,则会出现问题,因为这需要在结构内存中的某个位置使用虚拟表。 Normal member functions (such as a constructor) don't actually add any size to the struct. 普通成员函数(例如构造函数)实际上不向结构添加任何大小。 You can then pass the struct to the DLL with no problems. 然后,您可以毫无问题地将结构传递给DLL。

I would use a constructor class: 我会使用构造函数类:

struct Foo { ... };
class MakeFoo
{
   Foo x;
public:
   MakeFoo(<Required-Members>)
   {
     <Initalize Required Members in x>
     <Initalize Members with default values in x>
   }

   MakeFoo& optionalMember1(T v)
   {
      x.optionalMember1 = v;
   }

   // .. for the rest option members;

   operator Foo() const 
   {
     return x;
   }
};

This allows to arbitrary set members of the struct in expression: 这允许在表达式中任意设置结构的成员:

processFoo(MakeFoo(1,2,3).optionalMember3(5));

I have an easy idea, here is how: 我有一个简单的想法,这是如何:

Make the structure, just like you normally would, and create a simple function that initializes it: 像通常那样制作结构,并创建一个初始化它的简单函数:

struct Foo{...};

void Default(Foo &obj) {
    // ... do the initialization here
}

If you have multiple structures, you are allowed in C++ to overload the function, so you can have many functions called 'default', each initializing its own type, for example: 如果你有多个结构,你可以在C ++中重载函数,所以你可以有许多名为'default'的函数,每个函数都初始化它自己的类型,例如:

struct Foo { //... };
struct Bar { //... };

void Default(Foo &obj) {...}
void Default(Bar &obj) {...}

The C++ compiler will know when to call the first or the second overload based on the parameter. C ++编译器将根据参数知道何时调用第一个或第二个重载。 The & makes obj a reference to whatever parameter you give it, so any changes made to obj will be reflected to the variable you put as parameter. &使obj引用您给出的任何参数,因此对obj所做的任何更改都将反映到您作为参数放置的变量中。

Edit: I also have an idea for how to specify some parameters, you can do it by using default parameters. 编辑:我也知道如何指定一些参数,你可以使用默认参数。 This is how it works: 这是它的工作原理:

For example you the following function; 例如,你有以下功能; you can specify default values for parameters like this: 您可以为这样的参数指定默认值:

void Default (Foo &obj, int number_of_something = 0, int some_other_param = 10)
{ ... }

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

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