public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Overloading reference operator
@ 2010-04-25  3:44 Samkit Jain
  2010-04-25  4:57 ` Andrew Haley
  0 siblings, 1 reply; 6+ messages in thread
From: Samkit Jain @ 2010-04-25  3:44 UTC (permalink / raw)
  To: gcc-help

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2010-04-25 16:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-04-25  3:44 Overloading reference operator Samkit Jain
2010-04-25  4:57 ` Andrew Haley
2010-04-25  9:53   ` Andrew Haley
2010-04-25 13:39     ` Samkit Jain
2010-04-25 17:13       ` Andrew Haley
2010-04-26  6:13         ` Axel Freyn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).