From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 4642 invoked by alias); 24 Apr 2010 17:30:45 -0000 Received: (qmail 4622 invoked by uid 22791); 24 Apr 2010 17:30:41 -0000 X-SWARE-Spam-Status: No, hits=-1.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SARE_MSGID_LONG45,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: sourceware.org Received: from mail-px0-f175.google.com (HELO mail-px0-f175.google.com) (209.85.212.175) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sat, 24 Apr 2010 17:30:37 +0000 Received: by pxi14 with SMTP id 14so1060463pxi.20 for ; Sat, 24 Apr 2010 10:30:36 -0700 (PDT) Received: by 10.142.117.16 with SMTP id p16mr868814wfc.290.1272130236108; Sat, 24 Apr 2010 10:30:36 -0700 (PDT) MIME-Version: 1.0 Received: by 10.142.233.21 with HTTP; Sat, 24 Apr 2010 10:30:16 -0700 (PDT) From: Samkit Jain Date: Sun, 25 Apr 2010 03:44:00 -0000 Message-ID: Subject: Overloading reference operator To: gcc-help@gcc.gnu.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2010-04/txt/msg00262.txt.bz2 Hi, I overloaded reference operator in a templatized class like below: template MyClass { =A0public: =A0=A0 =A0MyClass() : m_data(10) { } =A0=A0 =A0operator T&() { return m_data; } =A0private: =A0=A0 =A0T m_data; }; Now I wrote a code to utilize reference operator as below: int main() { =A0=A0 =A0MyClass instance; =A0=A0 =A0instance++; // this works fine =A0=A0 =A0++instance; // this too works fine =A0=A0 =A0instance =3D 105; =A0 =A0// this gives compile error. Why ? =A0=A0 =A0return 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