6

Let's say I have the following struct:

struct myStruct
{
 int x;
 int y;
 int z;
 int w;
};

I want to initialize this struct to a default value when calling the following function. If it helps I'm looking for a simple zero initialization.

void myFunc(myStruct param={0,0,0,0})
{
 ...
}

This code however gives me compile error. I've tried VS2003 and VS2008.

NOTE: I have looked at other answers mentioning the use of constructor. However I want the user to see what values I'm using for initialization.

atoMerz
  • 6,868
  • 14
  • 56
  • 99
  • `myStruct param=myStruct{0,0,0,0}`? – HighCommander4 Mar 09 '13 at 06:27
  • @HighCommander4 nopes. – atoMerz Mar 09 '13 at 06:28
  • 3
    If you need fellow programmers to see the default values of `myStruct` (and that is the only reason you wish to define it as you have), I would add that information as a comment, rather than creating a default struct in the parameter list - a comment may be a little more elegant. Alternatively, you could instead pass a pointer to `myStruct` with a default param value of NULL, then check for NULL inside the method and create an empty struct if required. – Ephemera Mar 09 '13 at 06:28
  • @PLPiper I may end up doing that, but I was wondering if there was a way to fix this. – atoMerz Mar 09 '13 at 06:30
  • I assume the restriction to VS2003/VS2008 is the limiting factor for using an initializer list in your param set. What about a function? – WhozCraig Mar 09 '13 at 06:35
  • @PLPiper I *really* don't think pointer is the right way. I'd rather use a constructor and leave a comment instead. – atoMerz Mar 09 '13 at 06:36
  • Eh.. function isn't gonna work without providing params to it to fulfill the visible value clause of your question. which I suppose would work, but you're one step away from a constructor at that point. oh well. – WhozCraig Mar 09 '13 at 06:39
  • @jalf Mmmm.... Why didn't I think of this myself? Anyways... if you add this as an answer I'll accept. – atoMerz Mar 09 '13 at 12:27

2 Answers2

6

Adding default constructor in to your myStruct will solves your problem.

struct myStruct {
   myStruct(): x(0),y(0), z(0), w(0) { }   // default Constructor
   int x, y, z, w;
};

Function declaration:

void myFunc(myStruct param = myStruct());
SridharKritha
  • 5,151
  • 2
  • 34
  • 32
2

For modern C++ compilers which fully implement value-initilization it is enough to have the following value-initialized default value to zero-initiliaze data members of the myStruct:

myFunc(myStruct param=myStruct())

For other compilers you should to use something like this:

myStruct zeroInitilizer() {
   static myStruct zeroInitilized;
   return zeroInitilized;
}
myFunc(myStruct param=zeroInitilizer())

To avoid compiler specifics conside to use http://www.boost.org/doc/libs/1_53_0/libs/utility/value_init.htm

AnatolyS
  • 4,111
  • 14
  • 27
  • I'd say nowadays `zeroInitializer()` is not necessary, your first solution should work. Anyway I'd make it inline and returning copy of local const variable initialized to zero by ` = {}` .... – PiotrNycz Mar 09 '13 at 08:43
  • What is the purpose of the `static` inside the `zeroInitializer` function? – nwsteg Feb 28 '19 at 07:41