I love Haskell style pattern matching.
I have my C++ code as follows:
ObjectPtr ptr;
if(ptr.isType<Foo>()) { // isType returns a bool
Ptr<Foo> p = ptr.convertAs<Foo>(); // convertAs returns a Ptr<Foo>
......
}
if(ptr.isType<Bar>()) {
Ptr<Bar> p = ptr.convertAs<Bar>();
......
}
Now, are there any macros I can do define to simplify this? I have been pondering this for a while, but can’t simplify it further.
Thanks!
I’m assuming that your
Ptrtemplate has the concept of a NULL pointer.Though, as others have noted, switching on type is usually a sign you’re doing something wrong in C++. You ought to consider using virtual functions instead.