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 4075700
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:19:54+00:00 2026-05-20T17:19:54+00:00

In the following, I’m expecting obj to be converted to an int but why

  • 0

In the following, I’m expecting obj to be converted to an int but why does it returns operator + ambiguous in the following?

class MyClass
{
public:
    MyClass(int X = 0, double Y = 0):x(X), y(Y){}

    operator int() const  { return x; }
    operator double() const  { return y; }

private:
    int x;
    double y;
};

int main()
{
    MyClass obj(10, 20);

    int x = obj + 5; //obj converted to int
}
  • 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-20T17:19:55+00:00Added an answer on May 20, 2026 at 5:19 pm

    Because both conversions are equivalent, it’s ambiguous. Remember that the C++ language directly defines + for arguments of double and int, there is no standard conversion involved.

    So neither of these functions is better than the other:

    • double operator+ (double, int) — requires one user-defined conversion and no standard conversions
    • int operator+ (int, int) — requires one user-defined conversion and no standard conversions

    You’ll need to provide all the usual arithmetic operators yourself, if you want to make this work, and not rely on implicit conversion operators.

    • double operator+ (const MyClass&, int) — requires one standard conversion
    • int operator+ (const MyClass&, double) — requires no conversions

    Now obj + 5 will have an unambiguous best match.


    C++0x draft n3245 says, in section [over.built]

    • In this subclause, the term promoted integral type is used to refer to those integral types which are preserved by integral promotion (including e.g. int and long but excluding e.g. char). Similarly, the term promoted arithmetic type refers to floating types plus promoted integral types.

    For every pair of promoted arithmetic types L and R, there exist candidate operator functions of the form

    LR operator*(L,  R);
    LR operator/(L,  R);
    LR operator+(L,  R);
    LR operator-(L,  R);
    bool       operator<(L,  R);
    bool       operator>(L,  R);
    bool       operator<=(L,  R);
    bool       operator>=(L,  R);
    bool       operator==(L,  R);
    bool       operator!=(L,  R);
    

    where LR is the result of the usual arithmetic conversions between types L and R.

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

Sidebar

Related Questions

Following is my code to find middle node of double linked list but it
Following are the simple statements in the irb shell. What does nil in the
Following code worked fine abstract class FunctionRunnable<V> implements Runnable { protected abstract V calculate();
Following chunk html code works as expected: <iframe src=http://www.amazon.com/></iframe> But when trying embed inner
Following is my code: <table border=0 align=center cellpadding=0 cellspacing=0 class=grid_table> <tr class=grid_caption><td class=grid_caption>Customer Name</td></tr>
Following up from my last question does anyone know how I can use a
Following code works perfect and adds 1 and 2 values to the list, but
Following is the class which is repeated: <div class="inner"> <p> Text1 <span class="plus"> 25</span>
Following on from another question I have asked I have created a new class
Following code gives me warning at compile time: Warning: use of operator '>' has

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.