4
코드 줄을 봤는데 제대로 작동하지 않을 것이라고 생각하지 않았습니다. 조건부 연산자가 값을 반환하고 참조로 작동하지 않는다고 생각했습니다.조건부 연산자는 참조를 반환 할 수 있습니까?
일부 의사 코드 :
#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
}
누군가가 설명 할 수 있습니까? 고마워.