12

I'm still earning my C++ wings; My question is if I have a struct like so:

struct Height
{
    int feet;
    int inches;
};

And I then have some lines like so:

Height h = {5, 7};
Person p("John Doe", 42, "Blonde", "Blue", h);

I like the initialization of structs via curly braces, but I'd prefer the above be on one line, in an anonymous Height struct. How do I do this? My initial naive approach was:

Person p("John Doe", 42, "Blonde", "Blue", Height{5,7});

This didn't work though. Am I very far off the mark?

dreadwail
  • 15,098
  • 21
  • 65
  • 96
  • 4
    This may just be an example, but, it's probably better to store the `Height` as a single `inches` field. When you need feet you can convert that to feet; it's just far easier to deal with a single unit than to juggle multiple units, especially since as it is now you can have multiple values that are equal but do not have the same representation (e.g., `{2, 3}` and `{1, 15}`). – James McNellis Oct 26 '10 at 04:57
  • 1
    C++1x comes with [uniform initialization syntax](http://en.wikipedia.org/wiki/C%2B%2B0x#Uniform_initialization) which would support something like this. It's quite likely that your compiler already supports it. – sbi Oct 26 '10 at 05:15
  • 2
    Instead of `Height{5,7}` in the Person constructor, specify `(struct Height){5,7}` (if you're passing by reference) (or `&(struct Height){5,7}` if you're passing a pointer). You're basically casting the initializer list to the desired type. It's a C99+ feature, but should be usable with C++ as well. – Erhhung Jan 31 '14 at 07:25

2 Answers2

17

You can't, at least not in present-day C++; the brace initialization is part of the initializer syntax and can't be used elsewhere.

You can add a constructor to Height:

struct Height
{
    Height(int f, int i) : feet(f), inches(i) { }
    int feet, inches;
};

This allows you to use:

Person p("John Doe", 42, "Blonde", "Blue", Height(5, 7));

Unfortunately, since Height is no longer an aggregate, you can no longer use the brace initialization. The constructor call initialization is just as easy, though:

Height h(5, 7);
James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • Neat. I didn't know structs could have constructors/initializer-lists. – dreadwail Oct 26 '10 at 04:59
  • 11
    @byte: A `struct` is exactly the same thing as a `class`. The only difference is that the base classes and members are by default public for a `struct` and private for a `class`. – James McNellis Oct 26 '10 at 05:00
4

Standard C++ (C++98, C++03) doesn't support this.

g++ supports is a language extension, and I seem to recall that C++0x will support it. You'd have to check the syntax of the g++ language extension and/or possibly C++0x.

For currently standard C++, just name the Height instance, as you've already done, then use the name.

Cheers & hth.,

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • Is it automatically provided, or do you have to define a constructor that accepts an `initializer_list` argument? – Ben Voigt Oct 26 '10 at 05:08
  • @Ben: no, you don't have to define a constructor to get the C++0x syntax. much of the point/goal is to have a *uniform* syntax that works the same in most every situation. check out the [Wikipedia link](http://en.wikipedia.org/wiki/C%2B%2B0x#Uniform_initialization) given by @sbi earlier. Or, if you want the details, the latest draft is available from the [C++ committe pages](http://www.open-std.org/jtc1/sc22/wg21/), in PDF format. – Cheers and hth. - Alf Oct 26 '10 at 05:32