Weird error on templates‏

John S. Fine johnsfine@verizon.net
Mon Jul 12 17:37:00 GMT 2010


Panos C. C. wrote:
> I get a very weird error in a simple and pretty straightforward piece of code involving 2 templates.

>
>         RsrcPtr<Type>& operator=(RsrcPtr<Type> &a)
>         {
>             p = a.p;
>             a.p = NULL;
>         }
>
>   
...
>     RsrcPtr<B> niaou;
>     niaou = bs.foo();
>   

I think the main problem is taking a non const reference to a temporary 
object.

I don't quite understand the purpose of using RsrcPtr<Type> instead of 
just RsrcPtr inside the definition of

template<typename Type> class RsrcPtr

So I would have expected the operator= to be

        RsrcPtr& operator=(RsrcPtr const &a)
        {
            p = a.p;
            a.p = NULL;
            return *this;
        }

But if there was a reason for <Type> as you used it, just add the const and the return

        RsrcPtr<Type>& operator=(RsrcPtr<Type> const &a)
        {
            p = a.p;
            a.p = NULL;
            return *this;
        }




More information about the Gcc-help mailing list