You are here:

C++/parameterised constructor

Advertisement


Question
Sir,
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.

Answer
> I just want to know is whether there is any difference between working of these two?

There is no difference between the two as far as their working is concerned. The only difference between the two is that a non-explicit constructor acts as an implicit type-conversion operator and may be invoked implicitly; an explicit constructor will not be used for implicit conversions.

> How can we identify where to use implicit and where to use explicit?

In general, any constructor that can be invoked with a single argument should be declared explicit. The exception is: it is your design intent to facilitate an implicit conversion via the constructor.

For more information, see:
http://www.devx.com/tips/Tip/12493
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.22  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.