开发者

Parametrised and Conversion Constructors

开发者 https://www.devze.com 2023-01-02 04:10 出处:网络
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

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

精彩评论

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