|
|
Explicit and Implicit constructors in C++ Written on April 6, 2009, by Milot Shala. |
In this post I will write about explicit and implicit constructors which I find this information very useful while writing code.
Implicit constructors are constructors which can be a part of implicit conversion and explicit constructors cannot be converted implicitly.
I have written a program to demonstrate the behavior. Program contains a class which is kind of a string class and it’s constructor takes an integer as a parameter and then the same integer value is represented as string by the getNumberAsString() function.
Below is the header file which is a class declaration and declares two constructors first is the one that takes an integer as a parameter, the second is default constructor (with no parameters) and a function returning the string representation of the given integer (as mentioned in the paragraph above).
1 2 3 4 5 6 7 8 9 10 11 | // Header File class String { public: String(); String(int numberForConversion); string getNumberAsString(); private: int _tmp_num; }; |
In the definition of the String class below is implementation of the non-default constructor and the function returning the string representation of the number passed as a parameter to the constructor.
The constructor implementation is simple, it just compares the current value of the temporary private variable if not equal to the given value it assigns the given value to the private variable and the implementation of the function getNumberAsString() is simple, I’ve used the stringstream class from sstream STL library to do the conversion from integer to stream of strings and then return the actual string (notice the str() function).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // Source File String::String(int numberForConversion) { if(_tmp_num != numberForConversion) _tmp_num = numberForConversion; } string String::getNumberAsString() { stringstream tmp_stream; tmp_stream << _tmp_num; return tmp_stream.str(); } |
Now I’ve tested the class using main function. But in the main function you can see that I’ve passed number 10 to the constructor, then I assigned the value of 100 to the instance of the class, then I printed the actual value and the type name which is s for string. For the typeinfo library you can read more here.
1 2 3 4 5 6 7 8 9 10 | int main() { String *str = new String(10); *str = 100; string s_ = str->getNumberAsString(); cout << "Value: " << s_ << "\nType: " << typeid(s_).name() << endl; } |
But as you may expect the output to be 10 but it is going to be 100:
milot@lambda:~$ g++ -o test test.cpp milot@lambda:~$ ./test Value: 100 Type: Ss milot@lambda:~$
This is so because under the hood the compiler calls the constructor which takes an integer as a parameter, because implicitly we assigned an integer to the object’s instance, remember that if you implicitly assign a value to the object’s instance if you have a constructor that takes one parameter, like the constructor that I declared here, it would be called immediately after you assign the value thus type matches to the constructor’s parameter, and if you have constructors that takes multiple parameters, they are explicit by default.
But the guys of the C++ treated this behavior long time ago and they added a simple keyword to forbid this behavior which is explicit keyword, all you have to do is to put the explicit keyword in front of your class’s constructor as shown on the updated class declaration below:
1 2 3 4 5 6 7 8 9 10 | class String { public: explicit String(); explicit String(int numberForConversion); string getNumberAsString(); private: int _tmp_num; }; |
And your compiler will complain that you have a type mismatch like my gnu gcc compiler shown below:
milot@lambda:~$ g++ -o test test.cpp test.cpp: In function ‘int main()’: test.cpp:37: error: no match for ‘operator=’ in ‘* str = 100’ test.cpp:8: note: candidates are: String& String::operator=(const String&) milot@lambda:~$
Use explicit constructors if you find them useful in your code and happy hacking
-
Pietro
-
http://codespartan.org/blog/ Milot Shala
-
yrtz
5,331 views

