"we can initializate objects of a class for which we have not define
any constructor using:
- memberwise initialization.
- copy initialization.
- default initialization.
For example:
struct Work {
string author;
string name;
int year;
};
Work s9 { "Bethoven",
"Symphony No. 9 in D minor, Op. 125; Choral",
1824
}; // memberwise initialization
Work currently_playing {s9}; // copy initialization
Work none {}; // default initialization
The C++ Programming Language 4th Ed. Chapter 17.3.1
For example:
struct Data
{
int mMember1;
float mMember2;
char mMember3;
};
int main()
{
Data aData_1{1,0.3,33};
Data aData_2{aData_1};
return EXIT_SUCCESS;
}
This must work, althought I get a compiler error as much with GCC as with Clang. The error is "cannot convert Data to int" in both compilers. However, implementing the copy constructor this error disappear or without implenting it but using the round braces syntax. The problem is a little stupid and changing the curly for the round braces the problem get solved. But why the rules of TC++PL are not followed?, is a compilator issue or I'm misunderstanding something?. Thanks in advance.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…