First of all, private
members of base class are not accessible from derived class.
Now even if you fix that and make it protected
(or public
), then this will still be ill-formed, because you cannot initialize the base's members in the derived class's mem-init-list. It doesn't make sense, because by the time the derived class mem-init-list executes the base's members are already initialized, and the syntax : protectedElement(xyz)
will make the compiler think that protectedElement
is a member of the derived class, not of the base class!
See this error even after making it protected:
main.cpp: In constructor ‘Derived::Derived(std::vector<float>)’:
main.cpp:20:37: error: class ‘Derived’ does not have any field named ‘protectedElement’
Derived(std::vector<float> v) : protectedElement(v)
^
The right way to do this is to define a constructor for the base class, and call this from the derived class initialization-list.
Hope that helps.