The following code works
class A
{
public:
int i;
float f;
};
int main()
{
A a{ 1, 0.1 };
return 0;
}
However, if I add default values for A's members, it doesn't work
class A
{
public:
int i = 0;
float f = 3.14;
};
How to make both work together?
You have to define a default and a custom constructor like the example below:
class A
{
public:
A() {}
A(int const _i, float const _f) : i(_i), f(_f) {}
int i = 0;
float f = 3.14;
};
LIVE DEMO
However as already mentioned by @Kerek SB, @T.C. in the comments this will be fixed in C++14 and your code will work as is.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With