Home > Back-end >  With automatic type conversion of a copy of the initialization is how to?
With automatic type conversion of a copy of the initialization is how to?

Time:09-23

Struct hasY {
HasY ()=default;
HasY (int a) : (a) y {cout<" Normal direct initialization "& lt; HasY (hasY& Another) : y (another. Y) {cout<" Normal copy initialization "& lt;
int y;
};
Int main () {
HasY hy=3;
Coutreturn 0;
}
Results:
Normal direct initialization
3
The Program ended with exit code: 0
For hasY hy=3; This line, automatic type conversion should be first and call hasY (int a) construct a from the temporary object, and then use it to copy the initialization hy? If I'm wrong, welcome to spray! But the results did not call copy constructor? And if delete the copy constructor parameters of const inside, you will be prompted No viable constructor copying variable of type 'hasY', this also say clear practical to this function, because the generated temporary objects can only use const hasY& To receive, I am a rookie a, wish you a great god glad, thank you!!!!!!

CodePudding user response:

Because he was optimized, with Hy directly (int) to initialize the Hy

CodePudding user response:

During the copy initialization, the compiler is permitted (but not by) to skip the copy/move constructor and create the object directly. That is, the compiler is permitted to rewrite

String null_book="9-999-99999-9";//copy initialization into

String null_book (" 9-999-99999-9 ");//the compiler omits the copy constructor

An even if the compiler omits the call to the copy/move constructor, the copy/move constructor must exist and must be accessible (e.g., not private) at that point in the program. finally understand, thank you!
  • Related