1 Sales_data total; 2 total.isbn(); //等价于Sales_data::isbn(&total),编译器负责把total的地址传递给isbn的隐式形参this
this是一个常量指针,因为this总是指向“这个”对象,不允许改变this中保存的地址。
#include<iostream> using namespace std; class A { public: void f() { cout << "not const" << endl; } void f() const { cout << "const" << endl; } }; int main() { A a; cout << "a: "; a.f(); const A &b = a; cout << "b: "; b.f(); const A *c = &a; cout << "c: "; c->f(); A *const d = &a; cout << "d: "; d->f(); A *const e = d; cout << "e: "; e->f(); const A *f = c; cout << "f: "; f->f(); return 0; }
//output:
a: not const
b: const
c: const
d: not const
e: not const
f: const
原文:https://www.cnblogs.com/betaa/p/10577960.html