In the following code, it seems class C does not have access to A's constructor, which is required because of the virtual inheritance. Yet, the code still compiles and runs. Why does it work?
class A {};
class B: private virtual A {};
class C: public B {};
int main() {
C c;
return 0;
}
Moreover, if I remove the default constructor from A, e.g.
class A {
public:
A(int) {}
};
class B: private virtual A {
public:
B() : A(3) {}
};
then
class C: public B {};
would (unexpectedly) compile, but
class C: public B {
public:
C() {}
};
would not compile, as expected.
Code compiled with "g++ (GCC) 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)", but it has been verified to behave the same with other compilers as well.
For the second question, it is probably because you don't cause it to be implicitly defined. If the constructor is merely implicitly declared, there is no error. Example:
For your first question - it depends on what name the compiler uses. I have no idea what the standard specifies, but this code for instance is correct because the outer class name (instead of the inherited class name) is accessible:
Maybe the Standard is underspecified at this point. We'll have to look.
There does not seem to be any problem with the code. Furthermore, there is indication that the code is valid. The (virtual) base class subobject is default initialized - there is no text that implies that name lookup for the class name is dine inside the scope of
C
. Here is what the Standard says:12.6.2/8
(C++0x)And C++03 has similar text (thou less clear text - it simply says its default constructor is called at one place, and at another it makes it dependent on whether the class is a POD). For the compiler to default initialize the subobject, it just has to call its default constructor - there is no need to lookup the name of the base class first (it already knows what base is considered).
Consider this code that certainly is intended to be valid, but that would fail if this would be done (see
12.6.2/4
in C++0x)If the compiler's default constructor would simply look-up class name
A
inside ofC
, it would have an ambiguous lookup result with regard to what subobject to initialize, because both the non-virtualA
and the virtualA
's class-names are found. If your code is intended to be ill-formed, i would say the Standard certainly needs to be clarified.For the constructor, notice what
12.4/6
says about the destructor ofC
:This can be interpreted in two ways:
It seems to me that the Standard is less clear here. The second way would make it valid (by
3.4.3/6
, C++0x, because both class-namesA
are looked up in global scope), while the first will make it invalid (because bothA
will find the inherited class names). It also depends what subobject the search starts with (and i believe we will have to use the virtual base class' subobject as start point). If this goes likeThen we will directly find the virtual base' class name as a public name, because we won't have to go through the derived class' scopes and find the name as non-accessible. Again, the reasoning is similar. Consider:
If the destructor would simply call
this->A::~A()
, this call would not be valid because of the ambiguous lookup result ofA
as an inherited class name (you cannot refer to any non-static member-function of the direct base class object from the scopeC
, see10.1/3
, C++03). It will uniquely have to identify the class names that are involved, and has to start with the class' subobject reference likea_subobject->::A::~A();
.According to C++ Core Issue #7 class with a virtual private base can't be derived from. This is a bug in compiler.
Virtual base classes are always initialized from the most derived classes (C here). The compiler has to check that the constructor is accessible (i.e. I get an error with g++ 3.4 for
while your description implies there is none) but the fact that as a base, A is private or not doesn't matter (to subvert would be easy:
class C: public B, private virtual A
).The reason for which the virtual base classes constructors are called from the most derived class is that it needs to be constructed before any classes having them as a base class.
Edit: Kirill mentioned an old core issue which is at odd with my reading and the behavior of recent compilers. I'll try to get standard references in one way or the other, but that can takes time.