jeudi 26 avril 2018

Virtual inheritance constructor selection

Why does this print 20000? Code explicitly calls specific base constructors all the way up the inheritance train, yet ignores specified constructor and uses the default constructor instead.

#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;
}

Aucun commentaire:

Enregistrer un commentaire