Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
423 views
in Technique[技术] by (71.8m points)

c++ - Explicit keyword on multi-arg constructor?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In C++11 multi-parameter constructors can be implicitly converted to with brace initialization.

However, before C++11 explicit only applied to single-argument constructors. For multiple-argument constructors, it was ignored and had no effect.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...