AgPro

导航

explicit

explicit

When a constructor is specified as explicit, no automatic conversion will be used with that constructor -- but parameters passed to the constructor may still be converted. For example:

	struct foo 
	{   
		explicit foo( int a )  
			: a_( a )   
		{ 
		}  
		int a_; 
	};  
	int bar( const foo & f ) 
	{   
		return f.a_; 
	} 
	bar( 1 );           // fails because an implicit conversion from int to foo is forbidden by explicit.  
	bar( foo( 1 ) );    // works -- explicit call to explicit constructor. 
	bar( foo( 1.0 ) );  // works -- explicit call to explicit constructor with automatic conversion from float to int.

posted on 2010-06-11 15:07  AgPro  阅读(229)  评论(0编辑  收藏  举报