Does anyone knows how you can trick the C++ compiler to compile something like that (with the condition that TheObservedObject remains inside MyClass ):
template< typename Type >
class Observabile
{
public:
typename typedef Type::TheObservedObject TheObject;
void Observe( TheObject& obj ) {}
};
class MyClass : public Observabile< MyClass >
{
public:
class TheObservedObject
{
};
}
Sadly, that is not directly possible, as
MyClass, at the point of instantiation ofObservable, is not yet complete and as such you can’t access anytypedefs. You can work around this by adding a small wrapper:Or generelly just put
TheObservedObjectoutside ofMyClass(Wrapper).