3
#include<iostream>

class base{
public:
base(){std::cout<<"In base";}
};

class dv1:virtual private base {
public:
dv1(){std::cout<<"In DV1";}
};

class dv2:virtual private base {
public:
dv2(){std::cout<<"In DV2";}
};

class drv : public dv1, public dv2 {
public:
drv() {std::cout<<"Why is this working";}
};

int main() {
drv obj;

return 0;
}

Isn't in case of virtual inheritance, it is the responsibility of most derive class to call the constructor? Note: Here base is inherited virtually and privately.

Learner
  • 597
  • 1
  • 5
  • 17
  • and what is the output vs. what you were expecting? If you were expecting something different, I suggest you go back and revisit the chapter on inheritance and try to understand that first. – Nim Jan 05 '11 at 10:56
  • i expecting it not to compile as the base is derived virtually and Privately. – Learner Jan 05 '11 at 11:15

1 Answers1

5

Your constructor of drv didn't explicitly call the constructors of its base class(es), so the compiler will generate a call to the parameterless constructor of the base class

nos
  • 223,662
  • 58
  • 417
  • 506