What is the use of const overloading in C++?

前端 未结 5 2006
盖世英雄少女心
盖世英雄少女心 2020-12-12 14:30

In C++, a function\'s signature depends partly on whether or not it\'s const. This means that a class can have two member functions with identical signatures except that on

5条回答
  •  粉色の甜心
    2020-12-12 14:58

    #include 
    using namespace std;
    class base
    {
    
    public:
    void fun() const
    {
        cout<<"have fun";
    }
    void fun()
    {
        cout<<"non const";
    }
    
    };
    int main()
    {
        base b1;
        b1.fun(); //does not give error
        return 0;
    }
    

    Here compiler won't give any error, because in case of const functions compiler converts this pointer to const this*. this third argument separates these two functions.

提交回复
热议问题