3

I have a simple question but couldnt find the answer : what is the rule that the standard follows to choose between a const and non-const function.

Here is an example

#include <iostream>
class B{
int a;

public:

B(int _a) : a(_a){}

const int& getA() const{
    std::cout << "CONST\n";
    return a;
}

int& getA(){
    std::cout << "NON CONST\n";
    return a;
}

};

int main(){

B b(3);

b.getA(); //print non const
int a = 3 + b.getA(); //same
};

Both example use the non-const version. I thought it would pick the const version, since no modification is done.

0 Answers0