jeudi 1 novembre 2018

Cannot access protected constructor if using braced initializer [duplicate]

This question already has an answer here:

class Base
{
protected:
    explicit Base(int) {}
};

class Foo : public Base
{
    Foo()
        : Base(1)  // ok
    {}
};

class Bar : public Base
{
    Bar()
        : Base{1}  // Using braces gives me: protected function not 
                   //  accessible through a pointer or object
    {}
};

Why is it complaining about access only when I'm using {} to construct the base class? Is there a fundamental difference in using () vs {} in this context?

PS. there is no error if the Base constructor is public.

Aucun commentaire:

Enregistrer un commentaire