This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Why can not use reference in operator function?
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Parmenides <mobile dot parmenides at gmail dot com>
- Cc: gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Tue, 26 Nov 2013 13:34:39 +0000
- Subject: Re: Why can not use reference in operator function?
- Authentication-results: sourceware.org; auth=none
- References: <CAOXENUhNJbPC8F3ZYADpE970qOMxb4vOio+3c6LfV86hPV1evQ at mail dot gmail dot com> <CAH6eHdSdnUSiKeLEFaOMojHoVKMA01g1Huj3+2AEzrWLPmJqyg at mail dot gmail dot com> <CAOXENUgy1FadKfQd6uv88qoYPUMR3z9sx083up3cWkbCNTFwcg at mail dot gmail dot com> <CAH6eHdTHpCNtMb0EOrs034mifZ9j-r=CwyYgkTHM2J1uN6dmVg at mail dot gmail dot com> <CAOXENUiWsAvjwqNHbPA=9Ytx+KzVKyRkVrDM6qqtdrRix8VC7g at mail dot gmail dot com>
On 26 November 2013 13:17, Parmenides wrote:
> Hi Wakely,
>
> Thanks for your reply. The code has gotten compiled successfully with
> both compliers. I wonder what is the difference between 'Int &' and
> 'const Int &'.
That is a basic C++ question, so is not suitable for this mailing
list. See http://www.parashift.com/c++-faq/ref-to-const.html
> Why the former fails, while the latter causes the
> conversion from an 'int' to an 'Int' object?
Again, this mailing list is not the right place to try and learn C++.
The conversion from int to Int creates a temporary object, and
temporary Int objects cannot bind to Int& references, they can only
bind to const Int& reference. That's how C++ works.