荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: Version (Who makes history and why), 信区: Program
标  题: const member function
发信站: 荔园晨风BBS站 (Wed Apr  9 21:52:13 2003), 站内信件



Constant Member Functions
C++ Specific
Declaring a member function with the const keyword specifies that the functi
on 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 clo
sing parenthesis of the argument list. The const keyword is required in both
 the declaration and the definition. A constant member function cannot modif
y 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
}


--
                      *
          *                                  *
                          *             *
                      no more to say
                  ★     just wish you   ★
                            good luck

※ 来源:·荔园晨风BBS站 bbs.szu.edu.cn·[FROM: 192.168.1.50]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店