0

Why is my base class constructor called this this instance?

#include <iostream> using namespace std; class A { public: A() { cout << "A" << endl; } }; class B : public A { public: B() { cout << "B" << endl; } }; int main() { B b; getchar(); return 0; } 

The output is

A

B

Why is A called before B is? And why is A even called in the first place?

3
  • 2
    Because that's how constructors and classes work, in C++. Commented Dec 12, 2016 at 2:33
  • @SamVarshavchik so does it always call the base constructor and work it's way up each derived step? Commented Dec 12, 2016 at 2:34
  • See the linked question for the explanation. Commented Dec 12, 2016 at 2:34

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.