pingu
pingu

Reputation: 8827

Initialising c-style structs

I have a header that defines c style structs that are to be passed over a boundary on a c++ DLL. This header and DLL will potentially be used by c++, java and c applications. I want to initialise these structs in someway that allows the user to specify a subset of the parameters, and the rest will be given defaults.

I was thinking of creating a series of initialise functions in the header, which would take a reference to the struct they will initialise along with parameters for all members that can be set. The "initialise" functions would use overloading (based on the struct reference passed in) to ensure the correct one was called. I also planned on using default parameters to set the defaults. The functions would have to be gobal i guess.

Is this a good approach? Is there a better alternative? thanks

Upvotes: 2

Views: 293

Answers (1)

BЈовић
BЈовић

Reputation: 64233

You could add a function that returns a default initialized structure :

struct abc
{
  int a;
  float b;
  char c;
};

abc GetDefaultAbc()
{
  const abc def = { 1,2.0,3 };
  return def;
};

Upvotes: 6

Related Questions