const的object和 const member function
2014-01-14 23:31
351 查看
例子从 书上代码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
相关文章推荐
- Objective-C 单例模式写法以及单例模式模板宏
- 收集几个Objective-C的HTML解析库
- 初学objective c语言4 dealloc
- JAVA基础——Object类和Class类
- JSONKIT在Object C 中的实现小结
- jsonObject对象与本地自定义对象相互转化
- iOS Objective-C 解析XML SAX解析方法
- error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file
- Objective-C中的类型转换
- INT_MAX Maximum value for an object of type int 32767 (215-1) or greater
- c# XML-Object对象 序列化-反序列化
- 'MPD' object has no attribute 'myIP'
- Object-C的函数调用机制详解--消息
- StoreKit框架中几种重要的类及协议类图
- How to make more objects as same size in Visio 2010
- 绘制Objective-C程序的UML类图
- org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
- JSONObject.fromObject(map)(JSON与JAVA数据的转换)
- Objective C实现类似C++, Java中的范型
- 字符串分割与拼接