Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
只是偶然发现了我以前从未见过的东西。考虑到你有以下课程:
class foo { const bar* get() const; bar* get(); }
foo 的客户如何决定使用哪个 get() 方法?
就像任何其他重载一样const,它取决于调用函数的对象的访问路径(换句话说,取决于隐式this参数的类型)。
const
this
例子:
void bar(foo nc1, foo &nc2, foo *nc3, const foo &c1, const foo *c2) { // These call the non-const version: nc1.get(); nc2.get(); nc3->get(); // These call the const version: c1.get(); c2->get(); }