D
D
Dima Sokolov2018-03-22 18:58:02
C++ / C#
Dima Sokolov, 2018-03-22 18:58:02

C++ Constructors in inherited class?

If the base class has fields and a constructor that initializes them, then how to write a constructor for the inherited class? On msdn it is written that you need to specify all the parameters of the parent constructor, but this is not very convenient if the inherited class has completely different fields.
p.s. example from msdn doesn't compile

lass StorageBox : public Box {
public:
  StorageBox(int width, int length, int height, const string label&) : Box(width, length, height) {
    m_label = label;
  }
private:
  string m_label;
};

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman, 2018-03-22
@dimka11

const string label& -
compiles like this - const string &label

V
Vladimir, 2018-03-23
@mailwl

You can create a parameterless constructor for the base class, then you don't have to explicitly call it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question