On C++ constructors

I found the following piece of code in an open source implementation of a game.

struct Renderable {
    std::shared_ptr<UniformInput> uniform;
    std::shared_ptr<Geometry> geometry;
    bool alpha_blending = true;
    bool depth_test = true;
};


struct ShaderUpdate : Renderable {
    ShaderUpdate(std::shared_ptr<UniformInput> const& uniform)
        : Renderable{uniform, nullptr} {}

    ShaderUpdate(std::shared_ptr<UniformInput> && uniform)
        : Renderable{std::move(uniform), nullptr} {}
};

ShaderUpdate inherits from ‘Renderable’ and calls its base class constructor during object creation. But ‘Renderable’ does not have a 2-parameter constructor defined. Does the presence of “intialized” values in the struct have something to do with this?

Answer

Actually what’s going on here is that the Renderable is getting default constructed and then the values of uniform and geometry are being set. The compiler generates the default constructor and then you are just setting the values. I guess you are using c++14/11 or higher otherwise this won’t even work since older c++ versions don’t allow this.