-1

Is it necessary to call parent constructor with no arguments from child class constructor?

If I have a class A:

class A {
public:
    A() : value(100) { }
    int get_value() { return value; }
private:
    int value;
};

And a class B that inherets from A:

class B : public A {
public:
    B() : A() {}
};

Will the constructor of A be called when initializing an object of B even if I do not have: B() : A() {} and the value set to 100?

user644361
  • 272
  • 1
  • 5
  • 15
  • 1
    Yes, this is what standard constructors do. – Jodocus Mar 29 '19 at 13:47
  • @StackDanny I couldn't find the question, so I though in case someone else searches for it they don't have to try it themselves. It took me longer to write the question than to test it. – user644361 Mar 29 '19 at 13:49
  • @StackDanny That doesn't always work for c++. Because of the existence of undefined behavior you cannot assume that what you observe is well defined by the language. Learning c++ by trial and error is a huge source of problems. – François Andrieux Mar 29 '19 at 13:50
  • 1
    I'd suggest getting in the habit of using `class_name() = default;` if you want a default constructor. It "does the right thing" – NathanOliver Mar 29 '19 at 13:50
  • 1
    @user463035818 It's a helpful comment. If 90% of posters don't bother to check how their program behaves... this site will go downhill. – eerorika Mar 29 '19 at 13:50
  • @NathanOliver what if `B(T)` took an argument `T`? – user644361 Mar 29 '19 at 13:50
  • Please don't ask further questions in the comments, the whole thing becomes unmanageable. Either edit it into your question, or ask a new question. – Useless Mar 29 '19 at 13:51
  • 1
    Then you still need to write that. I'm just saying that instead of writting `B() : A() {}` for you default constructor, you use `B() = default;` instead. – NathanOliver Mar 29 '19 at 13:52
  • @eerorika if 90% of all posters would read the standard to answer their questions instead of asking here, the site would go downhill as well :P – 463035818_is_not_an_ai Mar 29 '19 at 13:52
  • 1
    don't really understand why my comment was removed? I think OP should really try it out on their own. Does it mean it's a bad question? no, but they could add to the question like asking "I tested it and the value is indeed 100 even if I don't call the constructor. But is this behaviour guaranteed?". – Stack Danny Mar 29 '19 at 13:56
  • @StackDanny that's true – user644361 Mar 29 '19 at 13:56
  • 1
    @StackDanny if this was the message you wanted to get across then there is nothing wrong about it, but your comment was suggesting to not ask here but instead get the answer by experimenting. Probably just unlucky wording ;) – 463035818_is_not_an_ai Mar 29 '19 at 14:14
  • @user463035818 yes, besides the lack of effort I was wrong by assuming it's not question-worthy. It definetely could be an interesting question. For sure! – Stack Danny Mar 29 '19 at 14:20

1 Answers1

1

Will the constructor of A be called when initializing an object of B even if I do not have: B() : A() {} and the value set to 100?

Yes.

Just like a member, if a base sub-object does not have an initialiser, it will be default-initialised. Default initialising A calls the user declared constructor.

You don't even need to declare the contsructor of B. The implicitly generated one does the same thing:

class B : public A {};
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • For reference, this is documented [here](https://en.cppreference.com/w/cpp/language/initializer_list). If that's not easy to read, I'd probably advise OP to read a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) rather than asking a question about every little uncertainty. That's a _very slow_ way of learning C++. – Useless Mar 29 '19 at 13:53