how does the compiler know if the 1st get() or the 2nd get() should be
called?
#include
using namespace std;
class X {
public:
X(int x = 0): dat(x) {}
void print() const { cout << dat << endl; }
void inc() { dat ++; }
private:
int dat;
};
class Y {
public:
Y(const X& x): dat(x) {}
Y(const Y& y) { dat = y.get(); }
X& get() { cout << "ref" <
const X& get() const { cout << "const ref" << endl; return dat;
}
private:
X dat;
};
int main()
{
X x(10);
Y y(x);
Y y2(y);
y.get().print();
y.get().inc();
y.get().print();
}
P********e 发帖数: 2610
2
我觉得除非有const lvalue才会用第二个。
而且,inc()肯定只能用1st get()
【在 g*********s 的大作中提到】 : how does the compiler know if the 1st get() or the 2nd get() should be : called? : #include : using namespace std; : class X { : public: : X(int x = 0): dat(x) {} : void print() const { cout << dat << endl; } : void inc() { dat ++; } : private: