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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:44:54+00:00 2026-05-16T06:44:54+00:00

If a class has a single argument constructor my understanding is it is implicitly

  • 0

If a class has a single argument constructor my understanding is it is implicitly convertible by the constructor to the type of the argument in appropriate contexts. Defining a conversion operator also makes a class convertible to another type. Questions

  • Does the conversion operator ever get called implicitly?
  • If both a single argument constructor and conversion operator with the same type are defined for a class does one have precedence over the other or is it ambiguous?
  • If you’ve decided that you want a class to be convertible to a given type, which approach is better or should you provide both?

Edit:

I see I didn’t understand clearly the directionality and that the two perform conversions in the opposite directions. As a follow on

  • If you have control over two classes that you want to make convertible to and from each other is there a preferred way in terms of these two operations to accomplish this?
  • Is it possible mark the conversion operator as explicit?
  • 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-16T06:44:55+00:00Added an answer on May 16, 2026 at 6:44 am

    No, if a class has a single argument constructor is implicitly convertible from the type of its argument.

    As for your other questions:

    • Does the cast operator ever get called implicitly?

    Yes, whenever it is needed.

    • If both a single argument constructor and cast operator with the same type are defined for a class does one have precedence over the other or is it ambiguous?

    I’m not too clear what you are asking, but if a conversion could go either way, it is ambiguous.

    • If you’ve decided that you want a class to be convertible to a given type, which approach is better or should you provide both?

    You have to use a cast – constructors don’t do this.

    In general, if you don’t want automatic conversions from a class to other types (and mostly you don’t), it is better to provide named conversion functions (ToString, ToInt, ToX) which will never be called automatically by the compiler.

    which leads on to your other two questions:

    • If you have control over two classes that you want to make convertible to and from each other is there a preferred way in terms of these two operations to accomplish this?

    Yes, used a named function to perform at least one of the conversions. std::string does this – there is a conversion from a char * to string using a constructor, but the other way you need to use the named c_str() function.

    • Is it possible mark the conversion operator as explicit?

    Unfortunately, no.

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

Sidebar

Related Questions

Given that I have a class Base that has a single argument constructor with
I'm trying to create a class, which has a constructor that takes a single
Currently I have created a ABCFactory class that has a single method creating ABC
String class has a constructor: new String(byte[] bytes, Charset charset) and a method: byte[]
If a class has a private constructor then it can't be instantiated. So, if
I have a class IPAddressString that has: a private field of type String called
If a class has inheritance in a Java program, do I need to put
My base class has properties that are used by a subclass. where should Release
A TimeSheetActivity class has a collection of Allocations. An Allocation is a value object
The Object class has a number of methods such as equals, hashCode, notify, wait

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.