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);
} 将印刷:
const foo int& int
我不明白为什么。为什么"const foo int& int“不会印刷?我认为常数对象只能调用常量方法,没有const可以调用none const。
发布于 2015-09-14 21:55:29
你误解了会员-const。
普通对象可以在其上调用任何成员函数,const或其他。
约束条件是您的const_a将无法在其上调用非const成员函数。不幸的是,你没有测试这个。
https://stackoverflow.com/questions/32574549
复制相似问题