tags:

views:

51

answers:

1

Is there any difference between parametrised constructor and conversion constructor. If so what is it?

+5  A: 

A parameterised constructor is (presumably) any constructor that takes one or more parameters. A conversion constructor is a constructor that can be called with a single parameter and is not declared explicit.

struct A {
    A();     // not parameterised or conversion
    A( int x, int y  ); // paramterised, not conversion
    A( int x );      // conversion
    explicit A( float z );    // not conversion;
};

Conversion constructors can be used by the compiler. Given:

void f( A a ) {
}

the compiler can call this function as:

f( 42 );

using the conversion constructor to convert 42 into an object of type A.

anon
@avakar Thanks for the correction - don't know what happened there!
anon