我遇到了一行代码,从没想过它会很好用。我认为条件运算符返回值并且不适用于参考。
一些伪代码:
#include <iostream>
using namespace std;
class A {
public:
A(int input) : v(input) {};
void print() const { cout << "print: " << v << " @ " << this << endl; }
int v;
private:
//A A(const A&);
//A operator=(const A&);
};
class C {
public:
C(int in1, int in2): a(in1), b(in2) {}
const A& getA() { return a;}
const A& getB() { return b;}
A a;
A b;
};
int main() {
bool test = false;
C c(1,2);
cout << "A @ " << &(c.getA()) << endl;
cout << "B @ " << &(c.getB()) << endl;
(test ? c.getA() : c.getB()).print(); // its working
}
有人可以解释吗?谢谢。