Explicit constructor taking multiple arguments

前端 未结 4 1116
面向向阳花
面向向阳花 2021-01-30 03:36

Does making a constructor having multiple arguments explicit have any (useful) effect?

Example:

class A {
    public:
        explicit A( in         


        
4条回答
  •  自闭症患者
    2021-01-30 04:23

    You'd stumble upon it for brace initialization (for instance in arrays)

    struct A {
            explicit A( int b, int c ) {}
    };
    
    struct B {
             B( int b, int c ) {}
    };
    
    int main() {
        B b[] = {{1,2}, {3,5}}; // OK
    
        A a1[] = {A{1,2}, A{3,4}}; // OK
    
        A a2[] = {{1,2}, {3,4}}; // Error
    
        return 0;
    }
    

提交回复
热议问题