常对象、对象的常引用与常指针访问类的成员函数与一般情况的不同之处

论坛 期权论坛 期权     
选择匿名的用户   2021-5-28 06:40   10415   0
#include <iostream>

using namespace std;

class Test {
public:
    void fun1();
    void fun1() const;
};

void Test::fun1()
{
    cout<<"normal member function"<<endl;
}
void Test::fun1() const
{
    cout<<"const member function"<<endl;
}
int main()
{
    cout << "Hello world!" << endl;
    Test t1;
    const Test t2;
    t1.fun1();        //访问普通成员函数
    t2.fun1();        //访问常成员函数
    const Test &t_r = t1;  //对象常引用
    t_r.fun1();       //访问常成员函数
    const Test *t_p = &t1; //常指针(指针指向的对象是常对象)
    t_p->fun1();      //访问常成员函数
    return 0;
}

转载于:https://www.cnblogs.com/rocklee25/p/7412615.html

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP