0

In C i can do this:

ppackage ppnull() {
    return (ppackage) {
        .type = NULL
    }
}

However, in C++ I get syntax errors. I use the GNU g++ compiler. Is there a switch to enable this?

Bart
  • 19,692
  • 7
  • 68
  • 77
imacake
  • 1,703
  • 7
  • 18
  • 26

1 Answers1

2

With c++11 you can use initializer list:

struct ppackage
{
    void* type;
};

ppackage ppnull()
{
    return {nullptr};
}

Or just

ppackage ppnull()
{
    return {};
}
Lol4t0
  • 12,444
  • 4
  • 29
  • 65
  • Putting `nullptr` in there is unnecessary – Seth Carnegie Feb 24 '12 at 19:11
  • Now ppackage has an anonymouse union with many structs inside it. There i must have `{.type='f', .fetch={"home"}}` – imacake Feb 24 '12 at 19:19
  • 1
    @imacake, well, this doesn't work with unions, you have to change syntax or reconsider your design (use constructors, but it makes your `struct` incompatible with `C`). You should tell, what you can do, than I can provide more complete solution. – Lol4t0 Feb 24 '12 at 19:38