
#include <iostream>#include <cstring>using namespace std;struct A{int v;A(int vv) : v(vv) {}const A *getPointer() const{return this;}};int main(){const A a(10);const A *p = a.getPointer();}
这个题实在是比较坑呀,坑太多了。
- 首先 const A * p = a.getPointer(); 是第一道坎,说明getPointer()这个方法应该是返回const A *
- 这还不够,因为它上面 const A a(10); 也是说明你这个getPointer()必须是一个常函数,因为常变量只能调用常函数
