<<< C++ struct, cont. | Index | C++ struct constructors >>> |
In our example we used initializer list to provide the initial values to the width and height of the rect object:
Rectangle rect { 20, 10 };
However, we may want to use other forms of the rectangle initialization:
Rectangle rect; // use default values Rectangle rect2( 100 ); // make it a square where width = height = 100
If we try
int main() { Rectangle rect; std::cout << "rectangle width: " << rect.width << '\n'; std::cout << "rectangle height: " << rect.height << '\n'; return 0; }
the output of width and height values yields undetermined result.
An attempt to create rect2 won't even compile:
Rectangle rect2( 100 ); // *** compiler error: no matching function for call
<<< C++ struct, cont. | Index | C++ struct constructors >>> |