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++程序设计课件(内部) 值得下载 Chapter 8: Constants Function arguments & return values, ...Constant objects, Constant member functions An example: complex number operation Mutable data member
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...
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, ...
Scoping Namespaces Nested Classes Nonmember, Static Member, and Global Functions Local Variables Static and Global Variables Classes Doing Work in Constructors Default Constructors Explicit ...
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 ...
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 ...
// NOTE - the ClassWizard will add and remove member functions here. //}}AFX_MSG DECLARE_MESSAGE_MAP() }; ///////////////////////////////////////////////////////////////////////////// //{{AFX_...
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 ...
% fmconst - Signal with constant frequency modulation. % fmhyp - Signal with hyperbolic frequency modulation. % fmlin - Signal with linear frequency modulation. % fmodany - Signal with arbitrary ...
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 ...
is_OracleDirectory Constant VarChar2(20) := 'ATTACH_DIR'; --内部附件生成目录(Oracle的目录) --Clob叠加比较慢,先用VarChar2叠加到4000个字符后才叠加到Clob字段 --UTL_INet.p_ClobCAT( Procedure...
7.12 Pointer to member conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 7.13 Function pointer conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...
Table of Contents Section 1 Introduction to SystemVerilog ...................................................................................................... 1 Section 2 Literal Values................
Hungarian Notation........................55 Variable Naming .......Constant Naming..............................................................57 Class Naming........................................