S
samjam86
Hi,
I overloaded reference operator in a templatized class like below:
template<class T>
MyClass
{
public:
MyClass() : m_data(10) { }
operator T&() { return m_data; }
private:
T m_data;
};
Now I wrote a code to utilize reference operator as below:
int main()
{
MyClass instance;
instance++; // this works fine
++instance; // this too works fine
instance = 105; // this gives compile error. Why ?
return 0;
}
The pre and post increment operators work fine, but a simple
assignment operator is giving compilation error.
Is there a valid reason for this or is this undefined behavior ?
Thank you.
Regards,
Samkit
I overloaded reference operator in a templatized class like below:
template<class T>
MyClass
{
public:
MyClass() : m_data(10) { }
operator T&() { return m_data; }
private:
T m_data;
};
Now I wrote a code to utilize reference operator as below:
int main()
{
MyClass instance;
instance++; // this works fine
++instance; // this too works fine
instance = 105; // this gives compile error. Why ?
return 0;
}
The pre and post increment operators work fine, but a simple
assignment operator is giving compilation error.
Is there a valid reason for this or is this undefined behavior ?
Thank you.
Regards,
Samkit