[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
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.

  1. include <assert>
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; }


Using explicit this should give a compiler warning, but under my environment it did not do so .

'explicit' links

Reference



last edited (November 15, 2006) by bilderbikkel, Number of views: 2260, Current Rev: 6 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.