public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Samkit Jain <samkit.feeds@gmail.com>
To: gcc-help@gcc.gnu.org
Subject: Overloading reference operator
Date: Sun, 25 Apr 2010 03:44:00 -0000	[thread overview]
Message-ID: <q2q202138ef1004241030xeed93ecax14d7792a83727744@mail.gmail.com> (raw)

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

             reply	other threads:[~2010-04-24 17:30 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-25  3:44 Samkit Jain [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=q2q202138ef1004241030xeed93ecax14d7792a83727744@mail.gmail.com \
    --to=samkit.feeds@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).