Default Memberwise Assignment

The assignment operator (=) can be used to assign an object to another object of the same type. By default, such assignment is performed by memberwise assignmenteach data member of the object on the right of the assignment operator is assigned individually to the same data member in the object on the left of the assignment operator. Figures 9.179.18 define class Date for use in this example. Line 20 of Fig. 9.19 uses default memberwise assignment to assign the data members of Date object date1 to the corresponding data members of Date object date2. In this case, the month member of date1 is assigned to the month member of date2, the day member of date1 is assigned to the day member of date2 and the year member of date1 is assigned to the year member of date2. [Caution: Memberwise assignment can cause serious problems when used with a class whose data members contain pointers to dynamically allocated memory; we discuss these problems in Chapter 11 and show how to deal with them.] Notice that the Date constructor does not contain any error checking; we leave this to the exercises.


Figure 9.17. Date class header file.

(This item is displayed on page 506 in the print version)

1 // Fig. 9.17: Date.h 2 // Declaration of class Date. 3 // Member functions are defined in Date.cpp 4 5 // prevent multiple inclusions of header file 6 #ifndef DATE_H 7 #define DATE_H 8 9 // class Date definition 10 class Date 11 { 12 public: 13 Date( int = 1, int = 1, int = 2000 ); // default constructor 14 void print(); 15 private: 16 int month; 17 int day; 18 int year; 19 }; // end class Date 20 21 #endif

Figure 9.18. Date class member-function definitions.

(This item is displayed on page 507 in the print version)

1 // Fig. 9.18: Date.cpp 2 // Member-function definitions for class Date. 3 #include 4 using std::cout; 5 using std::endl; 6 7 #include "Date.h" // include definition of class Date from Date.h 8 9 // Date constructor (should do range checking) 10 Date::Date( int m, int d, int y ) 11 { 12 month = m; 13 day = d; 14 year = y; 15 } // end constructor Date 16 17 // print Date in the format mm/dd/yyyy 18 void Date::print() 19 { 20 cout << month << '/' << day << '/' << year; 21 } // end function print

Figure 9.19. Default memberwise assignment.

(This item is displayed on pages 507 - 508 in the print version)

1 // Fig. 9.19: fig09_19.cpp 2 // Demonstrating that class objects can be assigned 3 // to each other using default memberwise assignment. 4 #include 5 using std::cout; 6 using std::endl; 7 8 #include "Date.h" // include definition of class Date from Date.h 9 10 int main() 11 { 12 Date date1( 7, 4, 2004 ); 13 Date date2; // date2 defaults to 1/1/2000 14 15 cout << "date1 = "; 16 date1.print(); 17 cout << " date2 = "; 18 date2.print(); 19 20 date2 = date1; // default memberwise assignment 21 22 cout << " After default memberwise assignment, date2 = "; 23 date2.print(); 24 cout << endl; 25 return 0; 26 } // end main  

date1 = 7/4/2004 date2 = 1/1/2000 After default memberwise assignment, date2 = 7/4/2004  

Objects may be passed as function arguments and may be returned from functions. Such passing and returning is performed using pass-by-value by defaulta copy of the object is passed or returned. In such cases, C++ creates a new object and uses a copy constructor to copy the original object's values into the new object. For each class, the compiler provides a default copy constructor that copies each member of the original object into the corresponding member of the new object. Like memberwise assignment, copy constructors can cause serious problems when used with a class whose data members contain pointers to dynamically allocated memory. Chapter 11 discusses how programmers can define a customized copy constructor that properly copies objects containing pointers to dynamically allocated memory.

Performance Tip 9.3

Passing an object by value is good from a security standpoint, because the called function has no access to the original object in the caller, but pass-by-value can degrade performance when making a copy of a large object. An object can be passed by reference by passing either a pointer or a reference to the object. Pass-by-reference offers good performance but is weaker from a security standpoint, because the called function is given access to the original object. Pass-by-const-reference is a safe, good-performing alternative (this can be implemented with a const reference parameter or with a pointer-to-const-data parameter).

Категории