开发者

Parametrised and Conversion Constructors

Is there any difference between parametrised const开发者_运维技巧ructor and conversion constructor. If so what is it?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜