I always thought, that there are only two defaults construcors: constructor with no arguments, and copy construtor.
But today I wrote something like this:
First I wanted to make sure that in C++ initialization of structures in c-style is still valid..
struct Foo{
int a;
bool b;
char* c;
double d;
};
//..
Foo arr[2]={{0, true, "a", 3.14}, {1, false, "z", 42.0}};
ok, this works. But next, I decided to check what will happen after changing struct
to class
.
class Bar{
public:
int a;
bool b;
char* c;
double d;
};
//..
Bar arr[2]={{0, true, "a", 3.14}, {1, false, "z", 42.0}};//works
Bar bar; //works
Bar bar2=arr[1]; //works
//Bar bar3(2, false, "so", 7.0); //of course doesn't work
//first, second, third ways works...
this is compilable as long as class Bar
doesn't have any private/protected fields (but it can contains methods). So, as long as compiler can create class which uses only simple features of structures, so long this can be compiled.
First question: Am I right?
Second question: Is it compiler's (gcc in this case) feature or this is exactly what Standard says?
[EDIT]:
code //Bar bar3(2, false, "so", 7.0); //of course doesn't work
is not an issue here ;)