![]() |
![]() |
![]() |
![]() |
![]() |
|
To define your own copy constructor for the railroad_car class, you
proceed as follows:
class railroad_car {
public: char *serial_number;
// Constructors:
railroad_car ( ) { }
railroad_car (char *input_buffer) {
// Create new array just long enough:
serial_number = new char[strlen(input_buffer) + 1];
// Copy string into new array:
strcpy (serial_number, input_buffer);
}
// Destructor:
virtual ~railroad_car ( ) {
delete [ ] serial_number;
}
// Other:
virtual char* short_name ( ) {return "rrc";}
virtual double capacity ( ) {return 0.0;}
private:
// Never-to-be-called copy-constructor prototype:
railroad_car (railroad_car&);
};
Note that it is enough to provide a function prototype; inasmuch as the function is never to be called, there is no need to provide a body.
It is also enough to provide a never-to-be-called copy-constructor function prototype in only the topmost class of every class hierarchy, because whenever any object in the class is copied, the topmost copy constructor has to be called.