// B2.7.cpp : Defines the entry point for the console application. // // Given the code fragments below, where the ellipsis (…) represents // code that has not been disclosed to you: // // class X { … }; class Y { public: explicit Y(const X& x); … }; // // what can you say about the compilation and execution of each of // the following statements? Describe each of the operations that // occur as this code executes. // // Y func(Y y) { … } // X x; // Y y = func(Y(x)); #include "stdafx.h" #include using namespace std; class X { public: X() { cout << "\n in X::X()"; } ~X() { cout << "\n in X::~X()"; } }; class Y { public: // main won't compile if any of these constructors are not defined Y() { cout << "\n in Y::Y()"; } Y(const X& x) /* : nothing to initialize */ { cout << "\n in Y::Y(const X& x)"; } Y(const Y& y) /* : nothing to initialize */ { cout << "\n in Y::Y(const Y& y)"; } Y& operator=(const Y& y) { // won't be called cout << "\n in Y::operator=(const Y&)"; } ~Y() { cout << "\n in Y::~Y()"; } }; Y func(Y y) { cout << "\n in func(y)"; cout << "\n type of my argument is " << typeid(y).name(); return y; } int _tmain(int argc, _TCHAR* argv[]) { std::cout << "\n Exam B2 - Question #7 " << "\n =======================\n"; cout << "\n\n-- first statement --"; Y y; func(y); cout << "\n\n-- second statement --"; X x; cout << "\n\n-- third statement --"; Y y2 = func(Y(x)); //// answer //////////////////////////////// // // as shown in the output of this program // //////////////////////////////////////////// cout << "\n\n"; return 0; }