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

* Re: Overloading reference operator
  2010-04-25  3:44 Overloading reference operator Samkit Jain
@ 2010-04-25  4:57 ` Andrew Haley
  2010-04-25  9:53   ` Andrew Haley
  0 siblings, 1 reply; 6+ messages in thread
From: Andrew Haley @ 2010-04-25  4:57 UTC (permalink / raw)
  To: Samkit Jain; +Cc: gcc-help

On 04/24/2010 06:30 PM, Samkit Jain wrote:
> template<class T>
> MyClass
> {
>  public:
>     MyClass() : m_data(10) { }
>     operator T&() { return m_data; }
>  private:
>     T m_data;
> };

I think this example does what you want:

template<class T> class
MyClass
{
public:
  MyClass() : m_data(10) { }
  operator T&() { return m_data; }
  T& operator= (const T &t) { m_data = t; return *this; }
private:
  T m_data;
};

Can you see why the definition of operator= does the trick?

Andrew.

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

* Re: Overloading reference operator
  2010-04-25  4:57 ` Andrew Haley
@ 2010-04-25  9:53   ` Andrew Haley
  2010-04-25 13:39     ` Samkit Jain
  0 siblings, 1 reply; 6+ messages in thread
From: Andrew Haley @ 2010-04-25  9:53 UTC (permalink / raw)
  To: Samkit Jain; +Cc: gcc-help

On 04/24/2010 07:07 PM, Andrew Haley wrote:
> On 04/24/2010 06:30 PM, Samkit Jain wrote:
>> template<class T>
>> MyClass
>> {
>>  public:
>>     MyClass() : m_data(10) { }
>>     operator T&() { return m_data; }
>>  private:
>>     T m_data;
>> };
> 
> I think this example does what you want:
> 
> template<class T> class
> MyClass
> {
> public:
>   MyClass() : m_data(10) { }
>   operator T&() { return m_data; }
>   T& operator= (const T &t) { m_data = t; return *this; }
> private:
>   T m_data;
> };

I'm sorry, I think I misunderstood your question.  You're asking why

  instance++;

works, but, say

  instance = instance + 1;

doesn't.  Apologies.

Andrew.

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

* Re: Overloading reference operator
  2010-04-25  9:53   ` Andrew Haley
@ 2010-04-25 13:39     ` Samkit Jain
  2010-04-25 17:13       ` Andrew Haley
  0 siblings, 1 reply; 6+ messages in thread
From: Samkit Jain @ 2010-04-25 13:39 UTC (permalink / raw)
  To: Andrew Haley; +Cc: gcc-help

Yes my question is why does'nt assignment operator work out of the box ?

On Sun, Apr 25, 2010 at 12:02 AM, Andrew Haley <aph@redhat.com> wrote:
> On 04/24/2010 07:07 PM, Andrew Haley wrote:
>> On 04/24/2010 06:30 PM, Samkit Jain wrote:
>>> template<class T>
>>> MyClass
>>> {
>>>  public:
>>>     MyClass() : m_data(10) { }
>>>     operator T&() { return m_data; }
>>>  private:
>>>     T m_data;
>>> };
>>
>> I think this example does what you want:
>>
>> template<class T> class
>> MyClass
>> {
>> public:
>>   MyClass() : m_data(10) { }
>>   operator T&() { return m_data; }
>>   T& operator= (const T &t) { m_data = t; return *this; }
>> private:
>>   T m_data;
>> };
>
> I'm sorry, I think I misunderstood your question.  You're asking why
>
>  instance++;
>
> works, but, say
>
>  instance = instance + 1;
>
> doesn't.  Apologies.
>
> Andrew.
>

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

* Re: Overloading reference operator
  2010-04-25 13:39     ` Samkit Jain
@ 2010-04-25 17:13       ` Andrew Haley
  2010-04-26  6:13         ` Axel Freyn
  0 siblings, 1 reply; 6+ messages in thread
From: Andrew Haley @ 2010-04-25 17:13 UTC (permalink / raw)
  To: gcc-help

On 04/25/2010 04:44 AM, Samkit Jain wrote:
> Yes my question is why does'nt assignment operator work out of the box ?

But why should it?  You haven't defined one.

Andrew.

P.S.  Please don't top-post.

> On Sun, Apr 25, 2010 at 12:02 AM, Andrew Haley <aph@redhat.com> wrote:
>> On 04/24/2010 07:07 PM, Andrew Haley wrote:
>>> On 04/24/2010 06:30 PM, Samkit Jain wrote:
>>>> template<class T>
>>>> MyClass
>>>> {
>>>>  public:
>>>>     MyClass() : m_data(10) { }
>>>>     operator T&() { return m_data; }
>>>>  private:
>>>>     T m_data;
>>>> };
>>>
>>> I think this example does what you want:
>>>
>>> template<class T> class
>>> MyClass
>>> {
>>> public:
>>>   MyClass() : m_data(10) { }
>>>   operator T&() { return m_data; }
>>>   T& operator= (const T &t) { m_data = t; return *this; }
>>> private:
>>>   T m_data;
>>> };
>>
>> I'm sorry, I think I misunderstood your question.  You're asking why
>>
>>  instance++;
>>
>> works, but, say
>>
>>  instance = instance + 1;
>>
>> doesn't.  Apologies.
>>
>> Andrew.
>>

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

* Re: Overloading reference operator
  2010-04-25 17:13       ` Andrew Haley
@ 2010-04-26  6:13         ` Axel Freyn
  0 siblings, 0 replies; 6+ messages in thread
From: Axel Freyn @ 2010-04-26  6:13 UTC (permalink / raw)
  To: gcc-help

Hi,
On Sun, Apr 25, 2010 at 02:39:09PM +0100, Andrew Haley wrote:
> On 04/25/2010 04:44 AM, Samkit Jain wrote:
> > Yes my question is why does'nt assignment operator work out of the box ?
> 
> But why should it?  You haven't defined one.

I believe the problem to be the following:
For the builtin-types, many candidate operator functions are already
predefined, for example

int& operator ++(int &);
int operator ++(int &, int);
int& operator =(int &, const int &);

for pre-increment, post-increment and assignment of integers.

Now, the way I understand overload resolution for 

MyClass<int> instance;

the following will happen:

a) "instance++;" or "++instance;" and all other unary/binary operators
except assignment operators:
Those can be either defined as member functions of MyClass, or by
non-member functions with one/two parameters. This allows the compiler
to find the predefined operator functions like int& operator ++(int &),
which can be used after converting MyClass<int> to int& using the
reference operator &.

b) "instance=1" oder "instance=instance+1" or any other assignment: 
Assignment operators have to be defined as member-functions of MyClass -
it is NOT possible to define assignment operators as
non-member-functions. Thus, during overload-resolution for an assignment
operators, the compiler does NOT try non-member functions, an thus it
does NOT find "int& operator =(int &, const int &);"


So as Andrew already suggested, it will only work when you define
explicitely as member-function an assignment operator which can be used
in this situation.


HTH,

Axel

> > On Sun, Apr 25, 2010 at 12:02 AM, Andrew Haley <aph@redhat.com> wrote:
> >> On 04/24/2010 07:07 PM, Andrew Haley wrote:
> >>> On 04/24/2010 06:30 PM, Samkit Jain wrote:
> >>>> template<class T>
> >>>> MyClass
> >>>> {
> >>>>  public:
> >>>>     MyClass() : m_data(10) { }
> >>>>     operator T&() { return m_data; }
> >>>>  private:
> >>>>     T m_data;
> >>>> };
> >>>
> >>> I think this example does what you want:
> >>>
> >>> template<class T> class
> >>> MyClass
> >>> {
> >>> public:
> >>>   MyClass() : m_data(10) { }
> >>>   operator T&() { return m_data; }
> >>>   T& operator= (const T &t) { m_data = t; return *this; }
> >>> private:
> >>>   T m_data;
> >>> };
> >>
> >> I'm sorry, I think I misunderstood your question.  You're asking why
> >>
> >>  instance++;
> >>
> >> works, but, say
> >>
> >>  instance = instance + 1;
> >>
> >> doesn't.  Apologies.
> >>
> >> Andrew.
> >>

^ 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).