There is a class Test:
typedef enum
{
AA, BB
} TType;
template <typename T>
struct TVector
{
typedef std::vector <T> Type;
};
template < typename T, const TType type >
class Test
{
private:
typename TVector <T>::Type it;
};
and its specialization with redefined operator = (there with no additional functionality):
template < typename T, const TType type >
class Test <T *, type>
{
public:
Test <T *, type > & operator = ( const Test <T*, type > &source ) {return *this;}
template <TType type2>
Test <T *, type > & operator = ( const Test <T*, type2 > &source ){return *this;}
template <TType type2>
Test <T *, type > * operator = ( const Test <T*, type2 > *source ) {return *this;}
};
I am trying to assign objects with different TType parameter each other, and this step works correctly.
int _tmain(int argc, _TCHAR* argv[])
{
Test <double *, AA> a1;
Test <double *, BB> b1;
a1=b1; //Correct
Test <double *, AA> *a2;
Test <double *, BB> *b2;
a2 = b2; //Error
return 0;
}
But the same step with pointers does not work, see the error code:
Error 1 error C2440: '=' : cannot convert from 'Test<T,type> *' to 'Test<T,type> *' 49
Is it possible to assign pointers with different TType parameter each other (how?) or not?
Updated question:
And what about assignment between pointers and objects?
a2 = &b1; //Error
*a2 = b1; //Unitialized memory
Could I ask for a code sample?
Thanks for your help.
The second example does not work because you are not assigning to the object, you are assigning to a pointer. It’s the same reason that this does not work:
Even though a
floatcan be assigned from anint, a pointer tofloatcannot be assigned from a pointer toint.Try
*a2 = b2or*a2 = *b2instead — your operators should catch both of those.Note also that this implementation appears to be wrong:
The
thisimplicit variable is already a pointer type, so you need toreturn this, notreturn *this. I would suggest eliminating this overload of the assignment operator completely, since it is bound to be more confusing than it will be useful.