I want to have a typedef in my base class to be specialized to each class derived from this base class. code:
template<class X>
class SharedPointer
{
public:
X* data;
SharedPtr(X *val)
{
data = val;
}
};
template<class T=Base> /* default type, I know this is a mistake.
The reason to have this here is to just indicate that the default argument
should be Base itself. so it'll have a Base type of shared pointer. */
class Base
{
public:
typedef SharedPointer<T> MyTypeOfPtr;
virtual MyTypeOfPtr Func()
{
Base *b = new Base;
return MyTypeOfPtr(b);
}
};
class Derived : Base<Derived>
{
public:
MyTypeOfPtr Func()
{
Derived *d = new Derived;
return MyTypeOfPtr(d);
}
};
main()
{
Base b;
Base::MyTypeOfPtr ptr1 = b.Func();
Derived d;
Derived::MyTypeOfPtr ptr2 = d.Func();
}
but this doesn't compile. is there a way to have this functionality?