16.4 VIRTUAL BASES AND COPY CONSTRUCTORS


16.4 VIRTUAL BASES AND COPY CONSTRUCTORS

Since the presence of a virtual base caused the downstream constructor definitions to be modified, an obvious next question is, What about the implementation of the downstream copy constructors? Yes, the downstream copy constructors also must now make direct calls to the copy constructor of the common base.

In the following extension of the program of the previous section, note the syntax of the copy constructors in line (D) and (E). Now there is direct invocation of the copy constructor for the indirect base X.

 
//VirtualBaseCopyConstruct.cc #include <iostream> using namespace std; class X { int x; public: X( int xx ) : x(xx) {} //copy constructor: X( const X& other ) : x( other.x ) {} //(A) virtual void print() { cout << "printing value of x of X subobject: " << x << endl; } }; class Y : virtual public X { int y; public: Y( int xx, int yy ) : X( xx ), y( yy ) {} //copy constructor: Y( const Y& other ) : X( other ), y( other.y ) {} //(B) void print() { X::print(); cout << "printing value of y of Y subobject: " << y << endl; } }; class T : virtual public X { int t; public: T( int xx, int tt ) : X( xx ), t( tt ) {} //copy constructor: T( const T& other ) : X( other ), t( other.t ) {} //(C) void print() { X::print(); cout << "printing value of t of T subobject: " << t << endl; } }; class Z : public Y { int z; public: Z( int xx, int yy, int zz ) : Y( xx, yy ), X(xx), z( zz ) {} //copy constructor: Z( const Z& other ): Y( other ), X( other ), z( other.z ) {} //(D) void print() { Y::print(); cout << "printing value of z of Z subobject: " << z << endl; } }; class U : public Z, public T { int u; public: U ( int xx, int yy, int zz, int tt, int uu ) : Z( xx, yy, zz ), T( xx, tt ), X( xx ), u( uu ) {} U( const U& other ) // copy constructor : Z( other ), T( other ), X( other ), u( other.u ) {} //(E) void print() { Z::print(); T::print(); cout << "printing value of u of U subobject: " << u << endl; } }; int main() { cout << "Z object coming up: " << endl; Z z_obj_1( 1110, 1120, 1130 ); z_obj_1.print(); //(F) cout << endl; cout << "Z's duplicate object coming up: " << endl; Z z_obj_2 = z_obj_1; z_obj_2.print(); //(G) cout << endl; cout << "U object coming up: " << endl; U u_obj_1(9100, 9200, 9300, 9400, 9500); u_obj_1.print(); //(H) cout << endl; //call U's copy constructor: cout << "U's duplicate object coming up: " << endl; U u_obj_2 = u_obj_1; u_obj_2.print(); //(I) cout << endl; return 0; }

This program produces the following output:

 Z object coming up:                          // output of line (F) printing value of x of X subobject: 1110 printing value of y of Y subobject: 1120 printing value of z of Z subobject: 1130 Z's duplicate object coming up:              // output of line (G) printing value of x of X subobject: 1110 printing value of y of Y subobject: 1120 printing value of z of Z subobject: 1130 U object coming up:                          // output of line (H) printing value of x of X subobject: 9100 printing value of y of Y subobject: 9200 printing value of z of Z subobject: 9300 printing value of x of X subobject: 9100 printing value of t of T subobject: 9400 printing value of u of U subobject: 9500 U's duplicate object coming up:              // output of line (I) printing value of x of X subobject: 9100 printing value of y of Y subobject: 9200 printing value of z of Z subobject: 9300 printing value of x of X subobject: 9100 printing value of t of T subobject: 9400 printing value of u of U subobject: 9500 




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net