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

The Archive Base Latest Questions

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

My question is regarding data type conversion in c++. Does c++ provides a implicit

  • 0

My question is regarding data type conversion in c++. Does c++ provides a implicit conversion for built-in data types ( int, float) to user defined data types?

In the following example, I am trying to add a double ( t4 = t3 + 1.0) with test object type and its working fine using + operator so is the double implicitly converted to test type object?

class test {
    double d;
    int m;

    public:
    test() {
        d=0;
        m=0;
    }

    test(double n) {
        d=n;
    }

    const test operator+(const test& t) {
        test temp;
        temp.d = d+ t.d;
        return temp;
    }
};

int main() {
    test t1(1.2);
    test t2(2.5);
    test t3, t4;
    t3= t1+ t2;
    t4 = t3 + 1.0;
    return 0;
}
  • 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-27T16:54:20+00:00Added an answer on May 27, 2026 at 4:54 pm

    The constructor test(double n) declares an implicit conversion from double to test. In general, any constructor that is callable with exactly one argument (that includes constructors that can take more arguments, but have default-values for those), can be used as an implicit conversion, unless it is marked explicit:

    struct Foo {
        Foo(int x); // implicit conversion from int to Foo
        explicit Foo(char c); // marked explicit - no implicit conversion
        Foo(std::string a, double pi = 3.14159); // can be used as implicit 
                                                 // conversion due to default 
                                                 // argument
    };
    

    Edit: t4 = 1.0 + t3 does not work, because you have overloaded your operator+ as a member-function, and thus it is only considered if the first operand is of the type test – implicit conversion are not tried in this case. To make this work, make your operator a free function:

    test operator+(const test& lhs, const test& rhs);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question regarding type conversion in C#. Object data_obj = test; string
I have a question regarding the some data which is being transfered from one
I asked a question a while back on here regarding caching data for a
Question regarding GtkBuilder. When we unref builder pointer does it destroys all the screens/widgets
Quick question regarding memory management in C++ If I do the following operation: pointer
Following my question regarding a .NET YAML Library ... as there doesn't seem to
i have a question regarding some complex data-binding. I want to be able to
Quick question regarding Automapper. I'm mapping data from an incoming web service, where datetimes
I was looking at the answer of this question regarding multiple generic types in
I have a question regarding to the code that OCRApiService.com provides to devs. This

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.