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

  • Home
  • SEARCH
  • 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 3695724
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:39:09+00:00 2026-05-19T04:39:09+00:00

When constructor is explicit, it isn’t used for implicit conversions. In the given snippet,

  • 0

When constructor is explicit, it isn’t used for implicit conversions. In the given snippet, constructor is marked as explicit. Then why in case foo obj1(10.25); it is working and in foo obj2=10.25; it isn’t working ?

#include <iostream>
class foo
{
    int x;
    public:
    explicit foo( int x ):x(x)
    {}
};

int main()
{
    foo obj(10.25);  // Not an error. Why ?
    foo obj2 = 10.25; // Error
    getchar();
    return 0;
}

error: error C2440: ‘initializing’ : cannot convert from ‘double’ to ‘foo’

  • 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-19T04:39:09+00:00Added an answer on May 19, 2026 at 4:39 am

    There is a difference between these two lines of code. The first line,

    foo obj(10.25);
    

    explicitly calls your foo constructor passing in 10.25. This syntax, in general, is a constructor call.

    The second line,

    foo obj2 = 10.25;
    

    tries to implicitly convert from 10.25 to an object of type foo, which would require use of an implicit conversion constructor. In this example, you’ve marked the constructor explicit, there’s no implicit conversion constructor available, and so the compiler generates an error.

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

Sidebar

Related Questions

Should a C++ (implicit or explicit) value constructor accept its parameter(s) by value or
By default, in C++, a single-argument constructor can be used as an implicit conversion
Is there any way to eliminate the explicit constructor call in Foo and somehow
I am trying to implement an explicit conversion constructor for an assignment and I
Constructor for PHP's exception has third parameter, documentation says: $previous: The previous exception used
Does a templated constructor (such as the following) override the implicit copy constructor? template
I have a constructor method similar to this: public class Foo { public Foo
I recently noticed a class in C++0x that calls for an explicit default constructor.
I've been curious about the impact of not having an explicit primary constructor in
In Scala, a class's primary constructor has no explicit body, but is defined implicitly

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.