This is an odd one. Note this is cut down example code, and misses out destructors deliberately).
template <class f, class g> class Ptr; class RealBase { }; template <class a, class b, class c = Ptr<a,b> > class Base : public RealBase { public: Base(){}; }; template <class d, class e> class Derived : public Base <d,e> { public: Derived(){}; void DerivedMethod(){}; }; typedef Derived<double,double> D_Derived; template <class f, class g> class Ptr { public: Ptr(){}; Ptr(Base<f,g,Ptr<f,g> >* a){in = a;}; Base<f,g,Ptr<f,g> >* operator->() { return in; }; Base<f,g,Ptr<f,g> >& operator*() { return *in; }; private: Base<f,g,Ptr<f,g> >* in; };
I’m using the Ptr class from this example in a vector, as a pointer to a Derived class.
As we can see, the Ptr takes a Base<>* as it’s constructor argument.
Unfortunately I need a constructor that takes a const Base<>*, and I cannot simply do this:
Ptr(const Base<>* a) { in = const_cast<Base<>*>(a)};
Any ideas how I can make this class accept a const Base<>* as it’s constructor?
Edit:
Ok, turns out I can fix this by making changes to unrelated code, so it’s a bit of a non-problem now =] Took me about a day to fix it though =[
I think you need to define a separate class to wrap pointers to const, since not only the arguments of the constructor, but also the return types of the operators should be changed to const versions. If you make the
ConstPtrafriendofPtr, this should work out quite nicely:To construct wrappers from raw pointers you could add an overloaded function, more or less like this: