Reputation: 9103
Look at this very basic C++ code:
class Class1 {
};
Class1 c1;
class Class2
{
public:
Class2(Class1 &c)
{
}
};
// Class2 abcd(c1); // OK outside declaration
class Class3
{
public:
Class2 abcd(c1); // Declaration of abcd as field -> error: unknown type name 'c1'
};
There is something i do not understand about abcd
declaration: It works if i declare it as a global variable. But i have a compiler error if i declare it as a field inside Class3
.
Upvotes: 4
Views: 79
Reputation: 172934
The default member initializer (since C++11) for non-static data member could be only used with brace or equals initializer, but not parentheses initializer.
Through a default member initializer, which is simply a brace or equals initializer included in the member declaration, which is used if the member is omitted in the member initializer list
If a member has a default member initializer and also appears in the member initialization list in a constructor, the default member initializer is ignored.
So you can
class Class3
{
public:
Class2 abcd{c1}; // brace initializer
Class2 abcd = Class2(c1); // equals initializer
};
Upvotes: 2