You are here:

C++/parameterised constructor


Yesturday I was reading about parameterised constructors. I read "There are two types of calling methods one is implicit and other is explicit." I know the basic difference of syntax between these two. I just want to know is whether there is any difference between working of these two? How can we identify where to use implicit and where to use explicit? I think there should be something different in there working as we need to define it diffeterntly. Can you specify how they works? and which one to use where and why?
Thanks in advance.

I am not 100% sure I have grasped what your quote is referring to but suspect it is the difference between explicitly creating an object of some type and having an object of some type created implicitly.

In C++ a type can have more than one constructor. Only a default constructor is not parameterised.

For an example let's look at the std::string type from the (ISO) C++ standard library.

This type has quite a few constructors, allowing us to create and empty string:

   std::string emprty_string;

Or initialised in various ways from one or more parameters, e.g., from a C sting (in fact a pointer to constant char):

   std::string from_C_string("Hello World!");

Or from a number and a character:

   std::string many_Xes(1234, 'X');


In fact there are (he counts them...) 8 ways to construct a std::string object (you can look up the details in any decent standard C++ library reference such as "The C++ Standard Library A Tutorial and Reference" by Niloai M. Josuttis)

All the examples so far show explicit requests to create std::string objects.

However, in C++ there are rules in place that allow conversion of one type from another implicitly where circumstances require it. These allow for example various integer and floating point types to be used with each other without the need for explicit conversion (although if such a conversion would loose information then many compilers will issue a warning, although this is _not_ a requirement of the C++ standard).

For user defined class types there are two mechanisms that allow converting between one type and another, depending on which direction the conversion is going as it were. To convert from an object of some user defined class type to some other type the class can define a conversion operator to that other type.

However, to convert from some other type to an object of this class type the class only requires a constructor that can accept a single parameter of the type to be converted from [or a (const) reference to such a type]. By accept a single parameter I mean it has a constructor that only has one parameter or it has a constructor in which at most one parameter has no default value.

For example if we have a function that requires a const reference to a std::string:

   std::string TransformString( std::string const & input_string ) { /* ... */ }

then we can call it with literal string values:

   std::string transformed_string( TransformString("transform this!") );

Even though the type of (narrow) literal strings in C++ is "array of n const char".
Now arrays in C and C++ 'decay' (the standard term) to pointers to their element type, which in the case of (narrow) string literals would be to pointers to const char, or char const * (or const char *).

As std::string has a constructor that accepts a char const * as its initialisation parameter, the compiler can implicitly convert a (narrow) literal string to a std::string, and pass this temporary std::string object (by reference to const object) to the TransformString function.

Note that if the parameter to TransformString were a plain reference to std::string then this conversion would not be allowed as an object passed by reference to non-const object can be modified during the function call and doing so on a converted value will only modify the temporary object created as a result of the conversion which will be destroyed at the end of the statement it was created in - not very useful and a potential point of very much confusion - so such usage is disallowed.

Note that such implicit conversions can occur in any expression not just for function parameters.

Of course such silent implicit conversions by the compiler may not be appropriate in all cases, and for such cases any constructor that could be used for such a conversion can be marked explicit:

   class HolderOfThings
   // ...
   // ...

   // Mark constructor as explicit to prevent it being considered for implicitly
   // converting size_t integer values to (temporary) HolderOfThings objects:

       explicit HolderOfThings( size_t number_of_things_held ) /* ... */

Hope this helps.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2017 All rights reserved.