Alex Goft
Alex Goft

Reputation: 1134

function overload of a constant object

struct A {

    void foo(int i, char& c) { 
        cout << "foo int char&" << endl;
    } 

    void foo(int& i, int j) const {
        cout << "const foo int& int" << endl;
    } 

}; 

int main() {   
    A a;   
    const A const_a;   
    int i = 1;   
    char c = 'a';   
    a.foo(i,i);
} 

Will be printed:

const foo int& int

I dont understand why. Why "const foo int& int" wont be printed? I thought that constant Object can only call constant methods, and none const can call none const.

Upvotes: 1

Views: 39

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385224

You misunderstood member-const.

A normal object can have any member function invoked on it, const or otherwise.

The constraint is that your const_a would not be able to have the non-const member function invoked on it. Unfortunately, you did not test that.

Upvotes: 1

Related Questions