views:

670

answers:

3
A: 

Ok what about this:

class BasePtr
{
public:
    virtual void* obj() = 0;
};

template <class T>
class Ptr :: public BasePtr
{
public:
    Ptr() : ptr(0) {};
    Ptr(T* a) : ptr(a) {};
    virtual T* obj() { return ptr; }
protected:
    T* ptr;
};

Use BasePtr in the Base class but pass it the correct template object when Base needs to use it.

template <class a, class b >
class Base
{
public:
    Base(){};
    void set_ptr( BasePtr* );
};

DDerived dd = DDerived();
Ptr<double,double> p(&dd);
dd.set_ptr( p );

I still do not quite understand your problem but I hope that helps.

David Allan Finch
Unfortunately impossible in the more complex scenario, as it ends up with a recursive template definition:template <class A, class B, class c = Ptr<A,B,Ptr<A,B . . . > > This is not an option, although it was the original design.
Ed Woodcock
Second revision:I understand your idea but it's not an option, the template argument is for a typedef, and so cannot be allocated after instantiation with a method.The templated argument is used to override the standard Ptr object with an MpiPtr when the code is used in a distributed fashion.
Ed Woodcock
Why can't MpiPtr derived from BasePtr?
David Allan Finch
A: 

Try to use template template parameter instead of default value.

template <class a, class b, template <class aa, class bb> class c = Ptr >
class Base
{
typedef Ptr<a, b> pointer;
public:
    Base(){};
};
Lazin
Erm. . .This doesn't compile and I'm not really sure what you're getting at? You seem to be trying to define a class inside a template argument.
Ed Woodcock
A: 

Unfortunately, I was being slightly retarded and had forgotten to put my Ptr class in the same namespace as the Base and Derived classes.

That, I guess, would be why it wasn't working ! =]

Ed Woodcock