class Base { public: void operator()() { func(); } private: virtual void func() {} }; class Derived1 : public Base { private: void func() override {} }; class Derived2 : public Base { private: void func() override {} };
Because I want to use operator overloading,
Link is a better option than a pointer.
What I intend to do is:
if (condition) { Base& obj = Derived1(); } else { Base& obj = Derived2(); }
But obj will be destroyed and completed.
Base& obj; if (condition) { obj = Derived1(); } else { obj = Derived2(); }
Will not work,
Because the link must be initialized when declared.
If I try:
Base& obj = condition ? Derived1() : Derived2();
Another mistake, since the ternary operator expects a convertible type.
What is the best solution to solve this problem?
source share