Initialize parent’s protected members with initialization list (C++)

It is not possible in the way you describe. You’ll have to add a constructor (could be protected) to the base class to forward it along. Something like:

class Parent
{
protected:
    Parent( const std::string& something ) : something( something )
    {}

    std::string something;
}

class Child : public Parent
{
private:
    Child() : Parent("Hello, World!")
    {
    }
}

Leave a Comment