From: Andrew Wall on 10 Dec 2009 11:25 "beemaster" <beemasterz(a)gmail.com> wrote in message news:c3c6f1f5-d866-47e7-a018-b10b339ce3ad(a)r40g2000yqn.googlegroups.com... >I have hierarchy, that contains one base interface class - > IConnection, > and two derived interfaces: IClientConnection and IServerConnection > > ================= > > class IConnection > { > public: > virtual void Send() = 0; > }; > > class IClientConnection : public virtual IConnection > { > public: > virtual void Connect() = 0; > }; > > class IServerConnection : public virtual IConnection > { > public: > virtual void Accept() = 0; > }; > > ================= > > I also have a realization for base interface > > ================= > > class Connection : public virtual IConnection > { > public: > virtual void Send() > { > //... > }; > }; > > ================= > > For two other interfaces I want to use existing realization of base > interface. > > ================= > > class ServerConnection : public IServerConnection, > piblic Connection > { > public: > virtual void Accept(); > }; > > ================= > > I inherit Connection, because I don't want to duplicate the code from > Connection::Send(); > > When I compile this code with Microsoft compiler, I get following > errors: > warning C4250: 'ClientConnection' : inherits > 'Connection::Connection::Send' via dominance. > Is there a better way of doing this? How can I avoid multiple > inheritance and code duplication? > I really like an idea of such inheritance. Why is it bad? > >From the source code you supply, I think you've got the hang of this multiple inheritance thing. The Microsoft warning C4250 is only a warning and not even that - its really just information which you knew anyway. When I've encountered this, I've just disabled the warning (after a lot of thought about it), and I recommend you do the same. IMHO this is exactly the way to use multiple inheritance - it avoids code duplication, but you have to accept fixed compile time and run time behaviour. Andrew Wall -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: anand on 16 Dec 2009 20:32 On Dec 10, 11:25 pm, "Andrew Wall" <wallguide-usen...(a)yahoo.com> wrote: > "beemaster" <beemast...(a)gmail.com> wrote in message > > news:c3c6f1f5-d866-47e7-a018-b10b339ce3ad(a)r40g2000yqn.googlegroups.com... > > > > >I have hierarchy, that contains one base interface class - > > IConnection, > > and two derived interfaces: IClientConnection and IServerConnection > > > ================= > > > class IConnection > > { > > public: > > virtual void Send() = 0; > > }; > > > class IClientConnection : public virtual IConnection > > { > > public: > > virtual void Connect() = 0; > > }; > > > class IServerConnection : public virtual IConnection > > { > > public: > > virtual void Accept() = 0; > > }; > > > ================= > > > I also have a realization for base interface > > > ================= > > > class Connection : public virtual IConnection > > { > > public: > > virtual void Send() > > { > > //... > > }; > > }; > > > ================= > > > For two other interfaces I want to use existing realization of base > > interface. > > > ================= > > > class ServerConnection : public IServerConnection, > > piblic Connection > > { > > public: > > virtual void Accept(); > > }; > > > ================= > > > I inherit Connection, because I don't want to duplicate the code from > > Connection::Send(); > > > When I compile this code with Microsoft compiler, I get following > > errors: > > warning C4250: 'ClientConnection' : inherits > > 'Connection::Connection::Send' via dominance. > > Is there a better way of doing this? How can I avoid multiple > > inheritance and code duplication? > > I really like an idea of such inheritance. Why is it bad? > > >From the source code you supply, I think you've got the hang of this > > multiple inheritance thing. > The Microsoft warning C4250 is only a warning and not even that - its really > just information which you knew anyway. When I've encountered this, I've > just disabled the warning (after a lot of thought about it), and I recommend > you do the same. > > IMHO this is exactly the way to use multiple inheritance - it avoids code > duplication, but you have to accept fixed compile time and run time > behaviour. > > Andrew Wall > I encountered the same problem today. But I got rid of the warning by making the base class functions as "virtual". The rationale behind that was: if the derived class needs to add some more functionality (in the future) than whats derived from the base, then this should allow it to do. But I wonder whether its a good practice or is it the correct way of doing? Cheers, Anand. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
First
|
Prev
|
Pages: 1 2 Prev: Variadic Templates – Recursion – Initializer Lists. Next: Templates conditional type |