What will be printed as a result of program execution?
#include <iostream>

struct Car
  Car() : price(20000) {}
  Car(double b) : price(b*1.1) {}
  double price;

struct Toyota : public virtual Car
  Toyota(double b) : Car(b) {}

struct Prius : public Toyota
  Prius(double b) : Toyota(b)  {}

int main(int argc, char** argv)
  Prius p(30000);

  std::cout << p.price << std::endl;

  return 0;
In virtual inheritance, the base class is initialized by the top member of the hierarchy. That is, if the base constructor is not explicitly specified in the initialization list of the child class, then the default constructor will be called, and any other call to the constructor of the base virtually inherited class in the middle of the hierarchy will be ignored. In this case, the Prius class constructor first of all implicitly calls the default constructor of the Car class. The explicit call of the Car constructor in the Toyota class is ignored.

Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Зарегистрируйся сейчас
или Подпишись на будущие тесты