Re: Initialization of base virtual classes
On 24 Nov., 23:04, sheffm...@mail.ru wrote:
[..]
Now, suppose we have a hierarchy:
class Node
{
public:
explicit Node(int a) { std::cout << "Node\n"; }
};
class Device: virtual public Node
{
public:
explicit Device(int b) : Node(b) { std::cout << "Device\n"; }
};
class Directory: virtual public Node
{
public:
explicit Directory(int c) : Node(c) { std::cout << "Directory
\n"; }
};
class HostDevice: virtual public Directory, virtual public Device
{
public:
explicit HostDevice(int d) : Node(d+1), Directory(d*2), Device
(d*3) { std::cout << "HostDevice\n"; }
};
HostDevice is both device and directory, but it represents one node,
therefore, Node must be virtual base class, and it is. Device and
Directory must also be virtual base classes because further extensions
of that hierarchy will contain classes which must represent one node
and must be derived from HostDevice and some other class, derived from
Directory and/or Device. Now, I want to describe a class USBHost like
this:
class USBHost: public HostDevice
{
public:
explicit USBHost(int e) : HostDevice(e) { std::cout << "USBHost
\n"; }
};
but I can't, cause according to standard I must initialize all virtual
base classes in most derived class, so the correct solution is like
this:
class USBHost: public HostDevice
{
public:
explicit USBHost(int e) : Node(e+1), Directory(e*2), Device(e*3),
HostDevice(e) { std::cout << "USBHost\n"; }
};
But what is wrong here is that I'm forced to repeat virtual base
construction logic, i.e: e+1, e*2, e*3 - all that is already written
in HostDevice initializer list and I have to rewrite that logic in
USBHost's initializer list. Is there a way to ease this ?
I do not see how such an omission of the initializer of
the virtual base class can be realized in your example,
because all your intermediate classes are non-abstract,
that is, I can create an object of these intermediate
types and such a construction requires the existence of
the offending initializer of the virtual base(s), because
now this very type is the most-derived class.
The situation becomes a bit more relevant, if these
virtual base classes would be abstract. There exists an active
issue
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#257
which describes the uselessness of the initializer
under these conditions.
Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]