I'm having some trouble with using the conditional operator to get a reference to an object. I have the a setup similar to this:
class D
{
virtual void bla() = 0;
};
class D1 : public D
{
void bla() {};
};
class D2 : public D
{
void bla() {};
};
class C
{
public:
C()
{
this->d1 = new D1();
this->d2 = new D2();
}
D1& getD1() {return *d1;};
D2& getD2() {return *d2;}
private:
D1 *d1;
D2 *d2;
};
int main()
{
C c;
D& d = (rand() %2 == 0 ? c.getD1() : c.getD2());
return 0;
}
When compiling, this gives me the following error:
WOpenTest.cpp: In function 'int
main()': WOpenTest.cpp:91: error: no
match for conditional 'operator?:' in
'((((unsigned int)rand()) & 1u) == 0u)
? c.C::getD1() : c.C::getD2()'
I understand this is illegal according to the C++ standard (as seen in this blog post), but I don't know how to get my reference to D
without using the conditional operator.
Any ideas?