tags:

views:

161

answers:

1

I recently came across some weird looking class that had three constructors:

class Class
{
    public:
        explicit Class(int );

        Class(AnotherClass );

        explicit Class(YetAnotherClass, AnotherClass );

    // ...
}

This doesn't really make sense to me - I thought the explicit keyword is to protect compiler chosen construction from a foreign type.

Is this allowed? If it it, what does it mean?

+8  A: 

explicit only applies to single-argument constructors. For multiple-argument constructors, it's ignored and has no effect.

It's probably left over from some earlier code: someone added another parameter to an existing explicit constructor.

Roger Lipscombe
Thanks for clarification. I'd figure GCC would warn for such an occasion.
LiraNuna
With the caveat that if all but one of the multi-arg params have default values then it will have an effect
zebrabox