C++ 之 const member function

一个常量成员函数(const member function), 可以读取类的数据成员,但不能修改类的数据成员。

1  声明

在成员函数声明的参数列表后,加上 const 关键字,将其声明为常量成员函数(const member function),表明其不被允许修改类的数据成员

下面定义了一个 Date 类,分别以年、月、日的形式来表示日期

class Date {
public:
    int day() const { return d; }
    int month() const { return m; }
    int year() const;
    void add_year(int n); // add n years
private:
    int d, m, y;
};

1) 如果常量成员函数,企图修改类的数据成员,则编译器会报错

// error : attempt to change member value in const function
int Date::year() const
{
    return ++y;
}

2) 当在类外面,定义常量成员函数时,const 关键字不可省略

// error : const missing in member function type
int Date::year()
{
    return y;
}

2  调用

一个常量成员函数,可以被 const 和 non-const 类对象调用; 而非常量成员函数(non-const member function),则只能被 non-const 型类对象调用。

void f(Date& d, const Date& cd)
{
    int i = d.year();    // OK
    d.add_year(1);        // OK
    int j = cd.year();    // OK
    cd.add_year(1);        // error
}

3  this 指针

<C++ Primer> 中,有关于 const 后缀更详细的解释:

this 指针默认是指向 non-const 型类对象的 const 型指针,因此,不能将 this 指针和 const 型类对象绑定,即 const 类对象无法调用类的成员函数。

在成员函数声明的参数列表后加 const 后缀,表明其 this 指针为指向 const 型类对象,如此, const 型类对象便可以调用常量成员函数了。

小结:

1) 类成员函数声明中的 const 后缀,表明其 this 指针指向 const 型类对象,因此该 const 类对象,可以调用常量成员函数(const member function)

2) 一个成员函数,如果对数据成员只涉及读操作,而不进行修改操作,则尽可能声明为常量成员函数

参考资料:

<C++ Programming Language_4th> ch 16.2.9.1

<C++ Primer_5th> ch 7.1.2

<Effective C++_3rd> Item 3

时间: 2024-12-29 03:23:12

C++ 之 const member function的相关文章

Const member functions in C++

Recently, I've started to review and learn C++ techniques. During the learning process, I followed a learn-by-example methodology, which I consider to be very effective. From now on, I'll use this blog to explain key points and interesting topics in

C++ 之const Member Functions

Extraction from C++ primer 5th Edition 7.1.2 The purpose of the const that follows the parameter list of an ordinary member function is to modify the type of the implicit this pointer. By default, the type of this is a const pointer to the nonconst v

static 和 no static Member function学习

以下是做实验的一段代码: #include <iostream> using namespace std; typedef void (*p)(); class Object { public: static void s_fun_1() { cout << "static function 1\n"; } void fun_1() {cout << "no static function 1\n";} }; typedef vo

Timer.4 - Using a member function as a handler

In this tutorial we will see how to use a class member function as a callback handler. The program should execute identically to the tutorial program from tutorial Timer.3. #include <iostream> #include <boost/asio.hpp> #include <boost/bind.

Nonstatic Member Function 的语意

C++ 的设计准则之一就是: nonstatic member function 至少必须和一般的 nonmember function 有相同的效率. 这就是说, 如果我们在以下两个函数之间做选择: float magnitude3d(const Point3d *_this){...} float Point3d::magnitude3d() const {...} 那么选择 member function 不因该有什么额外负担, 这是因为编译器内部已将 member 函数实体转换为对等的

C++对象模型——指向Member Function的指针 (Pointer-to-Member Functions)(第四章)

4.4 指向Member Function的指针 (Pointer-to-Member Functions) 取一个nonstatic data member的地址,得到的结果是该member在 class 布局中的byte位置(再加1),它是一个不完整的值,须要被绑定于某个 class object的地址上,才可以被存取. 取一个nonstatic member function的地址,假设该函数是nonvirtual,则得到的结果是它在内存中真正的地址.然而这个值也是不全然的,它也须要被绑定

C++ - 模板类模板成员函数(member function template)隐式处理(implicit)变化

模板类模板成员函数(member function template)隐式处理(implicit)变化 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24233693 指针支持隐式转换(implicit conversion), 在动态绑定中,派生类指针能够转换为基类指针. 可是模板的实例化(instantiations)之间, 是单独存在的, 派生类的实例化的模板(SmartPtr<Derived>), 不能转换为基类实例

Virtual Member Functions &amp; Static Member Function

如果一个 normalize() 是一个 virtual member function, 那么以下的调用: ptr->normalize(); 将会被内部转化为: (*ptr->vptr[1])(ptr); 其中:vptr 表示由编译器生成的指针, 指向 virtual table, 它被安插在每一个声明有(或继承自) virtual functinos 的 class object 中. 事实上其名称也会被 mangled, 因为在一个复杂的 class 派生体系中, 可能存在多个 vpt

Fatal error: Call to a member function read() on a non

选模版时报错如下:Fatal error: Call to a member function read() on a non-object inC:\wamp\www\DedCms5.7\include\dialog\select_templets.php on line 71或者是/include//templets/default/index.htm Not Found!95%的导致原因是本地测试好了之后上传到服务器的路径问题!解决方法:后台->系统->系统基本设置->核心设置 -