函数末尾加const的作用

#include <iostream>
using namespace std;
class Test
{
    int num;
public:
    Test()
    {
        num=10;
    }
    void print1()
    {
        cout<<num<<endl;
    }
    void print2() const
    {
        cout<<num<<endl;
    }
    void print3() const
    {
        num-=10;//ERROR 1.const函数内部不能修改成员变量
        cout<<num<<endl;
    }
};
int main()
{
    Test a;
    a.print1();
    a.print2();
    a.print3();

    const Test b;
    b.print1();//ERROR 2.const对象不能访问非const函数
    b.print2();
    b.print3();
    return 0;
}

 

posted @ 2017-07-13 16:10  三味线、  阅读(3167)  评论(0编辑  收藏  举报