Having the following class hierarchy, what will be printed to the console?
#include <iostream>

using namespace std;

class A
{
  int m_i;
public:
  A(int i) : m_i(i) { }
  void print()
  {
    cout << sizeof(m_i) << endl;
  }
};

class B : virtual public A
{
public:
  B(int i) : A(i) { }
};

class C : public B
{
public:
  C(int i) : B(i) { }
};

int main()
{
  C c(1);
  c.print();
  return 0;
}
Explanation
Constructors are called first (explicitly or implicitly) from the bottom class on the hierarchy for virtual classes, in this case, class C. Since the constructor isn't invoked explicitly, then the default constructor is invoked implicitly, which is not defined in the class A. Hence the code will not compile.

Слідкуй за CodeGalaxy

Мобільний додаток Beta

Get it on Google Play
Зворотній Зв’язок
Cosmo
Зареєструйся Зараз
або Підпишись на майбутні тести