Graeme
Graeme

Reputation: 4592

C-style struct initialization from initialization list in C++

Is there syntax that allows you to use the struct initialization syntax:

struct A { int a; int b; }

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

in an initialization list? E.g.

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

  private:
    A obj;
};

Upvotes: 3

Views: 3421

Answers (1)

kynnysmatto
kynnysmatto

Reputation: 3892

If you have a class with public member variables, you can automatically use it the same way as you would do with structs. But in C++ there's not a way to define arbitrary initializer list behavior. But in C++0x there is, as pointed out here. If you happen to be using GCC, this feature is supported in GCC 4.4 and above (if you compile with parameter -std=c++0x)

Upvotes: 4

Related Questions