|
|
以下源代码编译报错:
passing `const A' as `this' argument of `const int&
A::getInt()' discards qualifiers
- #include <iostream>
- class A
- {
- public:
- A() : member(5) {}
- const int& getInt() {
- return member;
- }
- private:
- int member;
- };
- class B
- {
- public:
- const A & getA() const {
- return a;
- }
- private:
- A a;
- };
- int main()
- {
- using namespace std;
- B b;
- b.getA().getInt();
- }
复制代码
如果A::getInt声明成 const int & getInt() const;就没问题。
如果我要用以上的两个class,而不修改它源码,应该怎么做呢? |
|