Initializing a C-style structure from an initialization list in C ++

There is a syntax that allows you to use the structure initialization syntax:

struct A { int a; int b; }

int main()
{
    A a = { 1, 2 }; //syntax
    return 0;
}

in the initialization list? For instance.

class B
{   
  public: 
    B(int a_, int b_) : obj { a_, b_ } { }

  private:
    A obj;
};
+3
source share
1 answer

If you have a class with public member variables, you can automatically use it the same way as for structures. But in C ++ there is no way to determine the arbitrary behavior of the list of initializers. But in C ++ 0x there is, as indicated here . If you use GCC, this function is supported in GCC 4.4 and higher (if you compile the -std = C ++ 0x parameter)

+4
source

Source: https://habr.com/ru/post/1787965/


All Articles