C ++ operator overload problem

I have my own SmartPointer class.

There are times when SmartPtr contains a class that inherits from the 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 works fine for the class itself, for example:

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

but not for a pointer to a class, I tried the following and it never gets a call and gets 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 overload? Thanks

+3
source share
1 answer

++: " -, - , , , . "

&test -, , . , , , , .

0

Source: https://habr.com/ru/post/1757195/


All Articles