用 const 限定类的成员函数

类的成员函数后面加 const,表明这个函数不会对这个类对象的数据成员(准确地说是非静态数据成员)作任何改变。

在设计类的时候,一个原则就是对于不改变数据成员的成员函数都要在后面加 const,而对于改变数据成员的成员函数不能加 const。所以
const 关键字对成员函数的行为作了更加明确的限定:有 const 修饰的成员函数(指 const
放在函数参数表的后面,而不是在函数前面或者参数表内),只能读取数据成员,不能改变数据成员;没有 const
修饰的成员函数,对数据成员则是可读可写的。

除此之外,在类的成员函数后面加 const 还有什么好处呢?那就是常量(即 const)对象可以调用 const 成员函数,而不能调用非const修饰的函数。正如非const类型的数据可以给const类型的变量赋值一样,反之则不成立。

请看下面一个完整的例子,然后我再作一些说明。

CODE:

[Copy to clipboard]

#include <iostream>
#include <string>
using namespace std;

class Student {
public:
  Student() {}
  Student( const string& nm, int sc = 0 )
    : name( nm ), score( sc ) {}

void set_student( const string& nm, int sc = 0 )
  {
    name = nm;
    score = sc;
  }

const string& get_name() const
  {
    return name;
  }

int get_score() const
  {
    return score;
  }

private:
  string name;
  int score;
};

// output student‘s name and score
void output_student( const Student& student )
{
  cout << student.get_name() << "/t";
  cout << student.get_score() << endl;
}

int main()
{
  Student stu( "Wang", 85 );
  output_student( stu );
}

设 计了一个类 Student,数据成员有 name 和 score,有两个构造函数,有一个设置成员数据函数
set_student(),各有一个取得 name 和 score 的函数 get_name() 和 get_score()。请注意
get_name() 和 get_score() 后面都加了 const,而 set_student() 后面没有(也不能有const)。

首先说一点题外话,为什么 get_name() 前面也加 const。如果没有前后两个 const 的话,get_name() 返回的是对私有数据成员 name 的引用,所以通过这个引用可以改变私有成员 name 的值,如

CODE:

[Copy to clipboard]

Student stu( "Wang", 85 );
  stu.get_name() = "Li";

即把 name 由原来的 "Wang" 变成了 "Li",而这不是我们希望的发生的。所以在 get_name() 前面加 const 避免这种情况的发生。

那么,get_name() 和 get_score() 这两个后面应该加 const的成员函数,如果没有 const 修饰的话可不可以呢?回答是可以!但是这样做的代价是:const对象将不能再调用这两个非const成员函数了。如

CODE:

[Copy to clipboard]

const string& get_name(); // 这两个函数都应该设成 const 型
int get_score();
void output_student( const Student& student )
{
  cout << student.get_name() << "/t"; // 如果 get_name() 和 get_score() 是非const成员函数,这一句和下一句调用是错误的
  cout << student.get_score() << endl;
}

由 于参数student表示的是一个对const Student型对象的引用,所以 student 不能调用非const成员函数如
set_student()。如果 get_name() 和 get_score() 成员函数也变成非const型,那么上面的
student.get_name() 和 student.get_score() 的使用就是非法的,这样就会给我们处理问题造成困难。

因此,我们没有理由反对使用const,该加const时就应该加上const,这样使成员函数除了非const的对象之外,const对象也能够调用它。

用 const 限定类的成员函数

时间: 2024-08-29 05:23:42

用 const 限定类的成员函数的相关文章

const修饰类的成员函数

<Effective C++>里面说,尽量使用const,const修饰变量一般有两种方式:const T *a,或者 T const *a,这两者都是一样的,主要看const位于*的左边还是右边,这里不再赘述,主要来看一下当const修饰类的成员函数时,成员函数有什么特点. 类的成员函数后面加 const,表明这个函数不会对这个类对象的数据成员(准确地说是非静态数据成员)作任何改变. 在设计类的时候,一个原则就是对于不改变数据成员的成员函数都要在后面加 const,而对于改变数据成员的成员函

类的成员函数后面加const有什么用(c++常问问题六)

每个类的成员函数都默认传入this指针,成员函数后面加了const后该成员函数将不能修改该类的成员了 class cat { public: cat(){}; string getName() const { this->m_strName = “”;//错误,const this不允许修改成员 return this->m_strName; //正确,没修改 } protected: string m_strName; }

作为类的成员函数,重载运算符只能有一个参数

1 overload a operator of a class, you can only use one para., this pointer is automatically used. class Rational { public: //not correct since this ponit would be used automatically. //Rational operator+ (const Rational& lhs, const Rational& rhs);

处理菱形继承问题&&实现一个虚函数的覆盖及调用&&实现以下几个类的成员函数

#include <iostream> #include <string> using namespace std; 1.实现以下几个类的成员函数 2.实现一个虚函数的覆盖及调用 3.处理菱形继承问题. 植物 class Botany { public: //(const string& name) // const char* name Botany(const char* name = "") :_name(name) //构造函数 { //cout

C++类的成员函数的指针和mem_fun适配器的用法

一.普通函数指针 我们先来看一个最简单的函数: void fool(int a) { cout << a<< endl; } 那么它的函数指针类型为: void (*) (int) 我们可以这样测试: void (*pFunc)(int) = &foo;//这里pFunc是个指针 pFunc(123); 这样就会打印出整数123:为了简化,我们可以使用typedef: typedef void (*pFunc)(int); 这里我们要说明一下: 这里的pFunc是 返回值为

在类的成员函数中调用delete this

在类的成员函数中能不能调用delete this?答案是肯定的,能调用,而且很多老一点的库都有这种代码.假设这个成员函数名字叫release,而delete this就在这个release方法中被调用,那么这个对象在调用release方法后,还能进行其他操作,如调用该对象的其他方法么?答案仍然是肯定 的,调用release之后还能调用其他的方法,但是有个前提:被调用的方法不涉及这个对象的数据成员和虚函数.说到这里,相信大家都能明白为什么会这样 了. 根本原因在于delete操作符的功能和类对象的

类的成员函数指针(比較深入)

From:http://blog.csdn.net/hairetz/archive/2009/05/06/4153252.aspx 个人感觉对于类的成员函数指针这块解说的比較深入具体 推荐阅读 ///////////////////////////////////////////////// 先看这样一段代码 class test {    public:       test(int i){ m_i=i;}       test(){} void hello()       {        

VB6/VBA中跟踪鼠标移出窗体控件事件(类模块成员函数指针CHooker类应用)

前几天发了一篇博文,是关于获取VB类模块成员函数指针的内容(http://www.cnblogs.com/alexywt/p/5880993.html):今天我就发一下我的应用实例. VB中默认是没有鼠标移出事件响应的,而这个事件其实在项目开发中,实用性很强,很多时候需要在鼠标移出窗体或控件时做些事情:没有这个事件会感觉很费力: 今天我所说的实际案例就是,在窗体上,设计一个SplitterBar控件,窗体的最终用户使用这个控件可以在运行程序时任意调整其内部控件大小. 我在第二篇参考博文作者开发的

C++类的成员函数存储方式(是否属于类的对象)

今天在看TAF源码的时候,发现下面一段有趣的代码: getSmallerProxyPrx = Application::getCommunicator()->stringToProxy<GetSmallerPrx>(MobileAssist.JiangeSmallerServer.GetSmaller); //此处T为GetSmallerPrx template<class T> T stringToProxy(const string& objectName,con