I've got a problem with casting an object instance by the value of its member. The test case:
class base {
public:
virtual int type() const = 0;
};
class derived : public base {
public:
virtual int type() const { return 1; }
virtual void derivedspecific() {}
};
int main() {
base* test = new derived;
(test->type()==1?((derived*)test):NULL)->derivedspecific();
}
Imagine, that we have hundreds of child classes and hundreds of cases in the ternary operator all written in a macro. How should I solve this problem? If i put more cases in the ternary operator i got error 'conditional expression between distinct pointer types'. And yeah, the test variable needs to be a base class pointer. It's just a short example.