// B6.cpp - describe all of the resulting operations #include using namespace std; class X { public: ////// compiler generated void and copy work correctly /////// // X() { cout << "\n constructing void base object"; } // X(const X &x) { cout << "\n copying base object"; } virtual void mf() { cout << "\n X::mf()"; } }; class Y : public X { public: ////// compiler generated void and copy work correctly /////// // Y() : X() { cout << "\n derived void construction"; } // Y(const Y &y) : X(y) { cout << "\n derived copy operation"; } virtual void mf() { cout << "\n Y::mf()"; } }; X aFunction(X &x) { x.mf(); return x; } // Describe everything that happens when the following code is executed? // Please be as complete as possible in describing what happens to the // objects x and y, below. void main() { ///////////////////////////////////////////////////////////////////////////// // In statement below: // - void compiler generated constructor for Y is called to initialize y; // - It calls, as its first act, compiler generated void constructor, X(), // for its base part. Y y; //////////////////////////////////////////////////////////////////////////// // In the statement below: // - y is passed by reference to aFunction, so no constructor is called here. // - x.mf() inside aFunction results in a call to Y::mf(). // - x, a reference to the derived object y, is returned by value, so a copy // is made using the compiler generated base class copy constructor // X(const X &s); // - A new X object, x, is created with a call to the X class copy constructor // - temporary X object is destroyed with X::~X(). // Note that compiler optimization will probably result in only one call to // the X copy constructor begin made for the return and creation of x. X x = aFunction(y); cout << "\n\n"; }