主题:关于函数的问题
john207
[专家分:0] 发布于 2002-07-19 18:31:00
请问:
在VC++的编程中经常会看到这样的函数定义,
例如:
CString GetToolTipText() const;
请问这const代表的含义?
回复列表 (共3个回复)
沙发
leapjet [专家分:0] 发布于 2002-07-20 10:27:00
这是一个常成员函数,在这个函数里,不能改变他所调用的对象的任何属性。一般用来返回一个值。
板凳
poplarc [专家分:0] 发布于 2002-08-12 08:45:00
建议老兄有时间看看msdn。
Constant Member Functions
C++ Specific
Declaring a member function with the const keyword specifies that the function is a "read-only" function that does not modify the object for which it is called.
To declare a constant member function, place the const keyword after the closing parenthesis of the argument list. The const keyword is required in both the declaration and the definition. A constant member function cannot modify any data members or call any member functions that aren't constant.
END C++ Specific
Example
// Example of a constant member function
class Date
{
public:
Date( int mn, int dy, int yr );
int getMonth() const; // A read-only function
void setMonth( int mn ); // A write function;
// cannot be const
private:
int month;
};
int Date::getMonth() const
{
return month; // Doesn't modify anything
}
void Date::setMonth( int mn )
{
month = mn; // Modifies data member
}
--------------------------------------------------------------------------------
Send feedback to MSDN.Look here for MSDN Online resources
3 楼
恶少老七 [专家分:110] 发布于 2007-10-11 15:52:00
英语不太行
只看懂了一小部分....
我来回复