utab wrote:
Dear all,
Can experienced users clarify the use of explicit keyword for ctors
with some examples?
Regards,
class A
{
public:
A(int x) : x_(x)
{}
int get_x() { return x_ ; }
private:
int x_ ;
} ;
int f(A a)
{
return a.get_x() ;
}
int main()
{
// Fine, class A can be implicitly constructed from an int.
A a1 = 5 ;
// Fine, class A can be implicitly constructed from an int.
f(3);
}
If you change class A's construct to be explicit, then A cannot be
implicitly constructed from an int, and both lines in main become errors.
--
Alan Johnson