LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-04-2012, 03:14 AM   #1
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
C++: Overloading overloaded operators


Code:
binaryOperators binaryOperators :: operator+ (const binaryOperators &right)
{
    return binaryOperators (*this + right.i);
}

binaryOperators operator+ (const binaryOperators &left, const binaryOperators &right)
{
    return binaryOperators (left.i + right.i);
}

int main ()
{
    binaryOperators obj (10);

    obj = 11 + obj;

    obj = obj + 11;

    return 0;
}
So, here the statement obj = 11 + obj; calls the function with explicit argument specification, and this one obj = obj + 11; calls the function which is the member of the class. Fine.

The problem is that second call results in an infinite loop.
What are the reasons and how to avoid that?
 
Old 01-04-2012, 03:51 AM   #2
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
Why
Code:
binaryOperators binaryOperators :: operator+ (const binaryOperators &right)
{
    return binaryOperators (*this + right.i);
}
and not
Code:
binaryOperators binaryOperators :: operator+ (const binaryOperators &right)
{
    return binaryOperators (this->i + right.i);
}
 
1 members found this post helpful.
Old 01-04-2012, 03:53 AM   #3
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
I get your point, but could you explain in detail why the previous call results in an infinite loop?
 
Old 01-04-2012, 06:26 AM   #4
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
This.i not this? You're recursively calling object + thing's value, not object's value + thing's value?
 
Old 01-04-2012, 07:25 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by Anisha Kaul View Post
I get your point, but could you explain in detail why the previous call results in an infinite loop?
If you got that point, you wouldn't still be asking that question. *this + right.i is a recursive call to the function containing it (using an implicit call to the constructor to turn right.i back into an object of type binaryOperators).

In addition, your choice of two versions of operator+ doesn't make sense. The first is a non const member function, so it will cover all the cases where the left operand is a non const binaryOperators object. The second is not a member function but takes const binaryOperators objects for left operand.

Why would you want that second function and why would you want the first one to only take non const left operands?

The typical reason for a non member version of operator+ is to take a left operand type that is not a object of the class you are working on.

Last edited by johnsfine; 01-04-2012 at 07:28 AM.
 
Old 01-04-2012, 07:51 AM   #6
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
I think that Anisha ask why compiler not warn about ambiguity between these two functions, when calling
Code:
binaryOperators + i_casted_for_binaryOperators
The first operator+ (class member) is something like
Code:
binaryOperators operator+ (binaryOperators left, const binaryOperators &right)
which probably for compiler is more appropriate then the second, and choose that version. But this is only my guess.
 
Old 01-04-2012, 08:16 AM   #7
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by eSelix View Post
I think that Anisha ask why compiler not warn about ambiguity between these two functions
I don't think Anisha was asking that. But since you are effectively asking it, I'll explain:

There is no ambiguity because one definition required a non const left operand and the other takes a const left operand.

When the left operand actually is const, the compiler can only use the definition that takes a const left operand. No hint of ambiguity there.

When the left operand is non const, a definition that takes a const left operand could be used (some ambiguity there) but the definition that takes a non const left operand is more specific. The ambiguity is resolved when one definition is more specific than the other.

Next consider the statement
Code:
    obj = 11 + obj;
The left operand is a const int and the right operand is a non const binaryOperators. No operator+ takes that pair of inputs, so the compiler must find implicit casts.

When you implicitly cast an integer to a binaryOperators, the result is a temporary object, and a temporary object passed by reference to a function is const. So even if the int had not been const, the binaryOperators object it becomes would be const.

The fact that the right operand is non const makes no difference because both operator+ definitions take only const right hand operands (so neither definition is a less specific fit to the right hand operand).

Only one definition fits the const left hand operand, so that definition is used.

Last edited by johnsfine; 01-04-2012 at 08:26 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] C++ Operator Overloading Within an Already Overloaded Operator mirlin510 Programming 8 04-17-2011 12:02 PM
Apache server getting overloaded by... what? philwynk Linux - Server 3 04-23-2010 04:31 PM
C++ template functions and overloaded operators PatrickNew Programming 4 08-06-2008 06:09 AM
Recovery from overloaded / JMCraig Linux - Newbie 2 04-01-2003 11:26 AM
CPU Overloaded jayakrishnan Linux - General 4 03-03-2003 12:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration