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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:22:29+00:00 2026-05-16T21:22:29+00:00

After reading this thread What does the explicit keyword mean in C++? I made

  • 0

After reading this thread
What does the explicit keyword mean in C++?

I made up this program

class MyClass
{
  public:
  explicit MyClass(int a)
  {
    cout<<"Int was called"<<endl;
    val = a;
  }

  MyClass(char *a)
  {
    cout<<"Char was called"<<endl;
    val = atoi(a);
  }

  MyClass(const MyClass& copy)
  {
    cout<<"Copy Const was called"<<endl;
    this->val = copy.val;
  }

  inline const int getval() const
  { return val; }

  private:
  int val ;
};

main code

int main()
{
  int x=4;
  char y='4';
  char *z = &y;

  MyClass a(x);
  MyClass b(z);
  MyClass c(a);
  MyClass d('4');

  cout<<a.getval()<<endl;
  cout<<b.getval()<<endl;
  cout<<c.getval()<<endl;
  cout<<d.getval()<<endl;

  return 0;
}

The output was:

Int was called
Char was called
Copy Const was called
Int was called
4
4
4
52

Now, as per thread above, it should throw error after the constructor call on object d but it didn’t.

g++ version info

Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) 

I am not sure if i have done something wrong in the above code.
Please help

  • 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-16T21:22:30+00:00Added an answer on May 16, 2026 at 9:22 pm

    Explicit constructor will not allow you to do something like “implicit conversion”, e.g. when initializing an object:

    MyClass d = 4;
    

    or when calling a function with parameters:

    void foo( const MyClass& param);
    
    ... 
    
    foo( 4);
    

    In your case, there is a conversion from char to int before calling the explicit constructor, which is as designed.

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

Sidebar

Related Questions

After reading this - What does 'synchronized' mean? I was still unable to understand
After reading this thread: NSUserDefaults not present on first run on simulator I know
After reading this thread: How to force browser to reload cached CSS/JS files? I
After reading many of the replies to this thread , I see that many
After reading this question, I need to clear up some things. IQueryable<Customer> custs =
After reading this article, it makes sense to rebase to gather changes from the
After reading this article I don't have a clear answer: http://palizine.plynt.com/issues/2010Oct/bypass-xss-filters/ Will browsers interpret
After reading this post I kinda felt in the same position as the guy
After reading this article on thedailywtf.com, I'm not sure that I really got the
After reading this: http://docs.djangoproject.com/en/dev/ref/contrib/csrf/#how-to-use-it I came to the conclusion that it is not valid

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.