`
weihe6666
  • 浏览: 430206 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Constant Member Functions

 
阅读更多
Constant Member Functions


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. A constant member function cannot modify any non-static data members or call any member functions that aren't constant.

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.

// constant_member_function.cpp
class Date
{
public:
   Date( int mn, int dy, int yr );
   int getMonth() const;     // A read-only function
   void setMonth( int mn );   // A write function; can't 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
}
int main()
{
   Date MyDate( 7, 4, 1998 );
   const Date BirthDate( 1, 18, 1953 );
   MyDate.setMonth( 4 );    // Okay
   BirthDate.getMonth();    // Okay
   BirthDate.setMonth( 4 ); // C2662 Error
}

分享到:
评论

相关推荐

    C++程序设计课件(内部)

    C++程序设计课件(内部) 值得下载 Chapter 8: Constants Function arguments & return values, ...Constant objects, Constant member functions An example: complex number operation Mutable data member

    Practical C++ Programming C++编程实践

    More on Classes Friends Constant Functions Constant Members Static Member Variables Static Member Functions The Meaning of static Programming Exercises 15. Simple Pointers const Pointers Pointers and...

    jira-misc-workflow-extensions-6.0.1.jar

    POST-FUNCTIONS: Assign to role member or to last role member Set Field Value to constant or Groovy expression Set field to user property Copy Field Value from Field to Field, from/to linked issues, ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Scoping Namespaces Nested Classes Nonmember, Static Member, and Global Functions Local Variables Static and Global Variables Classes Doing Work in Constructors Default Constructors Explicit ...

    C# 语言规格说明(English Edition第五版)

    6.1.9 Implicit constant expression conversions 112 6.1.10 Implicit conversions involving type parameters 112 6.1.11 User-defined implicit conversions 113 6.1.12 Anonymous function conversions and ...

    微软内部资料-SQL性能优化5

     Chapter 11: “Batches, Stored Procedures and Functions”, Inside SQL Server 2000 by Kalen Delaney Finding Rows without Indexes With No Indexes, A Table Must Be Scanned SQL Server keeps track ...

    rsa算法设计 密码学

    // NOTE - the ClassWizard will add and remove member functions here. //}}AFX_MSG DECLARE_MESSAGE_MAP() }; ///////////////////////////////////////////////////////////////////////////// //{{AFX_...

    Introduction_to_Optimum_Design.pdf

    14.5 Optimum Design of Two-Member Frame with Out-of-Plane Loads 481 14.6 Optimum Design of a Three-Bar Structure for Multiple Performance Requirements 483 14.6.1 Symmetric Three-Bar Structure 483 ...

    Matlab时频分析工具箱及函数应用说明

    % fmconst - Signal with constant frequency modulation. % fmhyp - Signal with hyperbolic frequency modulation. % fmlin - Signal with linear frequency modulation. % fmodany - Signal with arbitrary ...

    C++ 标准 ISO 14882-2011

    Contents Contents iii List of Tables xi List of Figures xv 1 General 1 1.1 Scope . . . . ....1.2 Normative references ....1.3 Terms and definitions ....1.4 Implementation compliance ....1.5 Structure of this ...

    Oracle P/L SQL实现发送Email、浏览网页等网络操作功能

    is_OracleDirectory Constant VarChar2(20) := 'ATTACH_DIR'; --内部附件生成目录(Oracle的目录) --Clob叠加比较慢,先用VarChar2叠加到4000个字符后才叠加到Clob字段 --UTL_INet.p_ClobCAT( Procedure...

    Google C++ International Standard.pdf

    7.12 Pointer to member conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 7.13 Function pointer conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...

    SystemVerilog Reference Manual 3.1a(中英文版)+最新SV IEEE 标准

    Table of Contents Section 1 Introduction to SystemVerilog ...................................................................................................... 1 Section 2 Literal Values................

    Tricks of the Windows video Game Programming---part1

    Hungarian Notation........................55 Variable Naming .......Constant Naming..............................................................57 Class Naming........................................

Global site tag (gtag.js) - Google Analytics