Q:

C++ Templates and operator overloading

I am learning templates and operator overloading. I have written some code but I am confused... Let me explain...
template <class T>
class tempType 
{
public:
    T value;
    bool locked;

    tempType():locked(false)
    { 
        value = 0; 
    }

    T operator=(T val)
    { 
        value=val;
        return value; 
    }
    tempType<T> operator=(tempType<T> &val)
    { 
        value=val.value; 
        return *this; 
    }
    operator T()
    { 
        return value; 
    }
};

And I did...
int main(void)
{
    tempType<int> i;
    tempType<bool> b;
    tempType<float> f;
    i.value = 10;
    i = i + f;
    return 0;
}

What code I need to write in order to execute
tempType<T> operator=(tempType<T> &val){}

Also, I why operator T() is required?

A:

template <class T>
class tempType 
{
public:
    T value;
    bool locked;

    tempType() : value(), locked(false)
    { 
        value = 0; 
    }

    //althought legal, returning something different from tempType&
    //from an operator= is bad practice
    T operator=(T val)
    { 
        value=val;
        return value; 
    }
    tempType& operator=(const tempType &val)
    { 
        value=val.value; 
        return *this; 
    }
    operator T()
    { 
        return value; 
    }
};

int main(void)
{
    tempType<int> a;
    tempType<int> b;
    a = b;
    return 0;
}

in the code, a = b calls the operator.
As for the second question, the operator T() is not needed "by default". In your example, it is used when you write i+f:

i is converted to an int
f is converted to a float
the operation (+) is performed
T tempType<int>::operator=(T val) is called for the assignement

