views:

16

answers:

0

I have my own SmartPointer class.

There are cases where SmartPtr contain a class that inherite from a Base class, and I would like to convert SmartPtr<ClassX> into SmartPtr<BaseClassOfClassX>;

I am trying to overload the SmartPtr Conversion operator to do this.

It work fine for the Class themself, such as:

template<class newType>
operator SmartPtr<newType>() const
{
    return SmartPtr<newType>((SmartPtr<newType>*)this);
}

but not for pointer to the Class, I have tried the following, and it never gets call and get the following error:

template<class newType>
operator SmartPtr<newType>*() const
{
        return static_cast<SmartPtr<newType>*>(this);
}

Simple code to get the error:

    SmartPtr<ClassX> test(pClassX);
    SmartPtr<BaseClassOfClassX>* ob = &test;

ERROR:
cannot convert from 'SmartPtr<T> *' to 'SmartPtr<T> *'

Does anyone see what is wrong with my second conversion overload? Thanks