I have a reference-counted base class which will be the parent of most of my objects. It has its own little reference counted methods. The trouble is, these might be subclassed. In a fashion, I’m trying to implement NSObject, but in C++.
This therefore means there’s an ‘init’ method.
Therefore, the init method should always return the child’s class type. Imagine this:
class BaseClass{
public:
virtual childclass* init();
}
I know in Objective-C this would be quite easy as it returns ‘id’ and no casting is required. However in C++ there’s no such thing as ‘id’ and therefore I’m hoping there’s a macro or something that automatically expands childclass* into the actual child’s class. I’m quite new to C++ so I’m not sure how to implement this.
This can be done via the Curiously Recurring Template Pattern.