例子从 书上代码copy的。例子说明了错误当我们用一个const object调用一个non const object的时候会编译错误。 提示can not convert 'this ' pointer from 'const Time' TO 'Time & ' 另外一个编译错误也是类似的
// Fig. 10.1: Time.h
// Time class definition with const member functions.
// Member functions defined in Time.cpp.
#ifndef TIME_H
#define TIME_H
class Time
{
public:
Time( int = 0, int = 0, int = 0 ); // default constructor
// set functions
void setTime( int, int, int ); // set time
void setHour( int ); // set hour
void setMinute( int ); // set minute
void setSecond( int ); // set second
// get functions (normally declared const)
int getHour() const; // return hour
int getMinute() const; // return minute
int getSecond() const; // return second
// print functions (normally declared const)
void printUniversal() const; // print universal time
void printStandard(); // print standard time (should be const)
private:
int hour; // 0 - 23 (24-hour clock format)
int minute; // 0 - 59
int second; // 0 - 59
}; // end class Time
#endif
// Time class member-function definitions.
#include <iostream>
#include <iomanip>
#include "Time.h" // include definition of class Time
using namespace std;
// constructor function to initialize private data;
// calls member function setTime to set variables;
// default values are 0 (see class definition)
Time::Time( int hour, int minute, int second )
{
setTime( hour, minute, second );
} // end Time constructor
// set hour, minute and second values
void Time::setTime( int hour, int minute, int second )
{
setHour( hour );
setMinute( minute );
setSecond( second );
} // end function setTime
// set hour value
void Time::setHour( int h )
{
hour = ( h >= 0 && h < 24 ) ? h : 0; // validate hour
} // end function setHour
// set minute value
void Time::setMinute( int m )
{
minute = ( m >= 0 && m < 60 ) ? m : 0; // validate minute
} // end function setMinute
// set second value
void Time::setSecond( int s )
{
second = ( s >= 0 && s < 60 ) ? s : 0; // validate second
} // end function setSecond
// return hour value
int Time::getHour() const // get functions should be const
{
return hour;
} // end function getHour
// return minute value
int Time::getMinute() const
{
return minute;
} // end function getMinute
// return second value
int Time::getSecond() const
{
return second;
} // end function getSecond
// print Time in universal-time format (HH:MM:SS)
void Time::printUniversal() const
{
cout << setfill( '0' ) << setw( 2 ) << hour << ":"
<< setw( 2 ) << minute << ":" << setw( 2 ) << second;
} // end function printUniversal
// print Time in standard-time format (HH:MM:SS AM or PM)
void Time::printStandard() // note lack of const declaration
{
cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 )
<< ":" << setfill( '0' ) << setw( 2 ) << minute
<< ":" << setw( 2 ) << second << ( hour < 12 ? " AM" : " PM" );
} // end function printStandard
#include "Time.h" // include Time class definition
int main()
{
Time wakeUp( 6, 45, 0 ); // non-constant object
const Time noon( 12, 0, 0 ); // constant object
// OBJECT MEMBER FUNCTION
wakeUp.setHour( 18 ); // non-const non-const
noon.setHour( 12 ); // const non-const
wakeUp.getHour(); // non-const const
noon.getMinute(); // const const
noon.printUniversal(); // const const
noon.printStandard(); // const non-const
} // end main