[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppExplicit
Make constructors that take a single 'size argument' explicit [1].
In the example below, a struct is initialized with a char, because of implicit type conversion.
Using explicit this should give a compiler warning, but under my environment it did not do so
.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppExplicit
(C++) explicit
Keyword to prevent implicit type conversion.Make constructors that take a single 'size argument' explicit [1].
In the example below, a struct is initialized with a char, because of implicit type conversion.
struct MyStringClass { MyStringClass(const int& length) : mLength(length), mCharacter(0) {} int mLength; char mCharacter; }; int main() { MyStringClass myClass('a'); //Does not what the class user intended assert(myClass.mLength == static_cast<int>('a')); return 0; }
- include <assert>
Using explicit this should give a compiler warning, but under my environment it did not do so
'explicit' links
Reference
- Bjarne Stroustrup. The C++ Programming Language (3rd edition) ISBN: 0-201-88954-4. Chapter 11.13.11: 'Make constructors that take a single 'size argument' explicit.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
