#include <iostream>
using namespace std;
class A
{
public:
int a;
A() : a(1234) {};
};
class B : virtual public A
{
public:
B() {a = 2345;};
};
class C : public A
{
public:
C() {a=3456;};
};
class E: virtual public A
{
public:
E() { a = 4567; };
};
class D: public B, public C
{
};
class F: public D, public E
{
};
void main()
{
F f;
//HERE cout << f.a;
cout << f.E::a << "\n";
cout << f.D::B::a << "\n";
cout << f.D::C::a << "\n";
}
1. When I comple the above code using cl.exe from microsoft command line compiler, it compiles find and the output is the following:
4567
4567
3456
When I compiler the above code using bcc32 (borland c++ command line compiler), I see the following
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
testmi.cpp:
Error E2090 testmi.cpp 41: Qualifier 'B' is not a class or namespace name in fun
ction main()
Error E2090 testmi.cpp 42: Qualifier 'C' is not a class or namespace name in fun
ction main()
*** 2 errors in Compile ***
Who is correct???
2. I should have one virtual instance F:::B::A::a and F::E::A::a and one non-virtual instance through F:::C::A::a
4567 is what E initialized a with, why do we see this? why no see what B initialized? How to initialize a in F or in D in the constructor?
3. How to make //HERE commented out code compile and work, and refer to the one-virtual A instance, with out doing any using?? (is that possible?)
2. The "E" part of "F" is initialized after the "D" part, and overwrites the value.
3. "F" contains two members with the name "a". If you want to refer to one of them without fully qualifying the name, you're going to have to tell the compiler which.
Why would you ever want to use something like this?
Bookmarks