Skip to main content
[Edit removed during grace period]
Source Link
Erik van Velzen
  • 7.2k
  • 3
  • 29
  • 27
deleted 68 characters in body
Source Link
Erik van Velzen
  • 7.2k
  • 3
  • 29
  • 27

You don't even need to define a constructor

struct foo {
    bool a = true;
    bool b = true;
    bool c;
 } bar;

To commentors belowclarify: why wouldn't I be able to use athese are called brace-or-equal-initializer here? Isn't this the same as below, a struct definition followed by an instantiationinitializers (because you may also use brace initialization instead of an object?equal sign). This is not only for aggregates: you can use this in normal class definitions. This was added in C++11.

struct foo {
    bool a = true;
    bool b = true;
    bool c;
};

foo bar;

You don't even need to define a constructor

struct foo {
    bool a = true;
    bool b = true;
    bool c;
 } bar;

To commentors below: why wouldn't I be able to use a brace-or-equal-initializer here? Isn't this the same as below, a struct definition followed by an instantiation of an object?

struct foo {
    bool a = true;
    bool b = true;
    bool c;
};

foo bar;

You don't even need to define a constructor

struct foo {
    bool a = true;
    bool b = true;
    bool c;
 } bar;

To clarify: these are called brace-or-equal-initializers (because you may also use brace initialization instead of equal sign). This is not only for aggregates: you can use this in normal class definitions. This was added in C++11.

deleted 1 characters in body
Source Link
Erik van Velzen
  • 7.2k
  • 3
  • 29
  • 27

You don't even need to define a consructorconstructor

struct foo {
    bool a = true;
    bool b = true;
    bool c;
 } bar;

To commentors below: why wouldn't I be able to use a brace-or-equal-initializer here? Isn't this the same as below, a struct definition followed by a an instantiation of an object?

struct foo {
    bool a = true;
    bool b = true;
    bool c;
};

foo bar;

You don't even need to define a consructor

struct foo {
    bool a = true;
    bool b = true;
    bool c;
 } bar;

To commentors below: why wouldn't I be able to use a brace-or-equal-initializer here? Isn't this the same as below, a struct definition followed by a an instantiation of an object?

struct foo {
    bool a = true;
    bool b = true;
    bool c;
};

foo bar;

You don't even need to define a constructor

struct foo {
    bool a = true;
    bool b = true;
    bool c;
 } bar;

To commentors below: why wouldn't I be able to use a brace-or-equal-initializer here? Isn't this the same as below, a struct definition followed by an instantiation of an object?

struct foo {
    bool a = true;
    bool b = true;
    bool c;
};

foo bar;
[Edit removed during grace period]
Source Link
Erik van Velzen
  • 7.2k
  • 3
  • 29
  • 27
Loading
added 10 characters in body
Source Link
Erik van Velzen
  • 7.2k
  • 3
  • 29
  • 27
Loading
added 226 characters in body
Source Link
Erik van Velzen
  • 7.2k
  • 3
  • 29
  • 27
Loading
Source Link
Erik van Velzen
  • 7.2k
  • 3
  • 29
  • 27
Loading