explicit specifier

   
explicit specifier Explicit constructor specifier

  function-specifier  := explicit 

The explicit specifier can be used with a constructor to prevent implicit type conversions. It is permitted for any constructor but makes sense only for constructors that can be called with a single argument. An explicit constructor can be invoked from a declaration that uses function-like initialization or from an explicit type cast but not from a declaration that uses assignment-like initialization, nor from an implicit type cast.

Example

 struct point {  explicit  point(int x, int y = 0); }; point p1(10);  // OK point p2 = 10; // Error: would be OK without explicit point p3; p3 = 20;       // Error: would be OK without explicit p3 = static_cast<point>(40); // OK 

See Also

class , declaration , function , static_cast , type , Chapter 2, Chapter 6

   


C++ in a Nutshell
C++ in a Nutshell
ISBN: 059600298X
EAN: 2147483647
Year: 2005
Pages: 270
Authors: Ray Lischner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net