I have the following classes:
class Socket { Socket(); Socket( SOCKET s ); }; class Connection : public virtual Socket { Connection( IP ip ); };
These two classes contain some pure virtual functions and some non-virtual functions and some of their own data. Their essence is that I will get several types of sockets, implementing different protocols.
Therefore, I specialize in these two classes:
class ProtocolSocket : public virtual Socket { ProtocolSocket() {} ProtocolSocket( SOCKET s ) : Socket( s ) { ; } }; class ProtocolConnection : public ProtocolSocket, public virtual Connection { ProtocolConnection( SOCKET s, IP ip ) : ProtocolSocket( s ), Connection( ip ) {;} };
Something went wrong - as I am sure, many of you see. I am trying to create a ProtocolConnection:
new ProtocolConnection( s, ip );
Construction continues as follows:
start ctor ProtocolConnection start ctor Connection start ctor Socket Socket(); - default ctor via Connection init list end ctor Socket Connection(); - default ctor ProtocolConnection init list end ctor Connection start ctor ProtocolSocket start ctor Socket // Socket( s ); - skipped!!! - would have been from init // list of ProtocolSocket, but ctor for this object // already called! end ctor Socket ProtocolSocket( s ); -from init list of ProtocolConnection() end ctor ProtocolSocket ProtocolConnection( s, ip ); end ctor ProtocolConnection
Skipping this second Socket constructor is what the language specification says, and for good reason.
How to make it invoke a constructor named Socket (s), not an earlier one?
I intend to have several derived classes, for example. Also, OtherProtocolSocket and OtherProcolConnection, at the same level as the ProtocoSocket and ProtocolConnection objects.
The effect that I am trying to achieve, I want to build the ProtocolSocket and ProtocolConnection objects, and then transfer them around my system as Socket and Connection objects. Therefore, after I created the socket, it implements this protocol, I just read and write it without worrying about the details of the underlying protocol.
Connection objects must inherit all methods from socket objects.
@UPDATE:
DyP suggests adding an initializer for Socket in ProtocolConnection. This solves the problem. I would give you consent to this ... but it was just in the comment.