Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7027567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:15:54+00:00 2026-05-28T00:15:54+00:00

class binaryOperators { public: int i; binaryOperators (int tempI = 0) { i =

  • 0
class binaryOperators 
{
    public:
        int i;

        binaryOperators (int tempI = 0)
        {
            i = tempI;
        }

        binaryOperators operator+ (const binaryOperators &right);
};

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:15:54+00:00Added an answer on May 28, 2026 at 12:15 am

    The conversion from binaryOperators::i (of type int) to binaryOperators is implicit (i.e. not declared explicit).

    return binaryOperators (*this + right.i); // (1)
    binaryOperators binaryOperators :: operator+ (const binaryOperators &right); // (2)
    binaryOperators operator+ (const binaryOperators &left, const binaryOperators &right); // (3)
    

    In the line (1) two operator+ functions can be considered: The member version (2) and the free version (3). Since the LHS is of type binaryOperators&, the member version is applicable and preferred. Its argument is of type const binaryOperators &, and the argument given in line (1) is of type int, so the compiler tries to convert int to const binaryOperators &.

    Since there is a non-explicit constructor with one argument, it is used as an implicit conversion from int to const binaryOperators &. Now we have two operands of types binaryOperators& and const binaryOperators &, the operator+ in (2) can be called, and we are right where we started.

    Lesson: Don’t over-do implicit conversions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> #include <fstream> using namespace std; class binaryOperators { public: int i; binaryOperators
{ class Program { static void Main(string[] args) { int id = 0, stock
class C { public: C(C& c) { i = c.i; j = 100; }
class ZiggyTest{ public static void main(String[] args){ RunnableThread rt = new RunnableThread(); Thread t1
class ZiggyTest{ public static void main(String[] args) { String str = aaaaaaaaabb; String []
class person { var $name; var $email; //Getters function get_name() { return $this->name; }
// class class MyClass { public: void doIt() const { cout << It works!
class Person { string name; public Person(string name) { this.name = name; } public
class someclass {}; class base { int a; int *pint; someclass objsomeclass; someclass* psomeclass;
class AbstractQuery { virtual bool isCanBeExecuted()=0; public: AbstractQuery() {} virtual bool Execute()=0; }; class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.