|
|||
|
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? -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|
||||
|
||||
|
|
|
|||
|
beemaster wrote:
> 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 IServerConnection : public virtual IConnection > { > .... > class Connection : public virtual IConnection > { > public: > virtual void Send() > { > //... > }; > .... > class ServerConnection : public IServerConnection, > piblic Connection > { > .... > ================= > > 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. > .... > I have read up on this warning on MSDN and tried it out and I don't quite get it. Note that this warning is *only* generated for virtual and not for non-virtual functions. The compiler warns me that unqualified Send() calls on objects of type ServerConnection will always resolve to Connection::Send func but this is what should and will happen with virtual dispatch anyway! (Note the case where Send would be non-virt and where the warning might make some sense is not covered by C4250. Note also that it is irrelevant for this warning if Send is pure virtual in IConnection.) So I only can ask: Why does the compiler warn me about something that's supposed to happen anyway? cheers, Martin -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
On Dec 10, 12:02 am, beemaster <beemast...@gmail.com> wrote:
> 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? Technically, there's no problem and this will work. Conceptually, you mixed two concerns (responsibilities): connection and data exchange. You might want to read about single responsibility principle http://www.objectmentor.com/resources/articles/srp.pdf. You stepped on this principle. Goran. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
On Dec 10, 12:02 am, beemaster <beemast...@gmail.com> wrote:
<snip> > 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? No. The warning basically tells you that the Send method that is visible through the IServerConnection resolves to the Send method of Connection. This is exactly the behaviour that you want, so this is one of those few warnings that you can safely ignore/disable. > How can I avoid multiple > inheritance and code duplication? > I really like an idea of such inheritance. Why is it bad? > Bart v Ingen Schenau -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
beemaster wrote:
> > 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? > It's bad because Microsoft's compiler writers know more than you do about what you intended to do. If you disagree with their assessment, turn off the [stupid] warning. -- Pete Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The Standard C++ Library Extensions: a Tutorial and Reference" (www.petebecker.com/tr1book) [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
"beemaster" <beemasterz@gmail.com> wrote in message news:c3c6f1f5-d866-47e7-a018-b10b339ce3ad@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! ] |
|
|||
|
On Dec 10, 11:25 pm, "Andrew Wall" <wallguide-usen...@yahoo.com>
wrote: > "beemaster" <beemast...@gmail.com> wrote in message > > news:c3c6f1f5-d866-47e7-a018-b10b339ce3ad@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! ] |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Multiple Inheritance Interfaces and Code Reuse | Timie Milie | Newsgroup comp.language.c++ | 4 | 10-05-2009 09:27 AM |
| Multiple Inheritance Update | Doug Hoffman | Newsgroup comp.lang.forth | 0 | 08-02-2009 02:16 PM |
| A situation where private inheritance is useful | Juha Nieminen | Newsgroup comp.language.c++ | 4 | 07-18-2009 06:00 AM |
| ANS Compatible Multiple Inheritance | Doug Hoffman | Newsgroup comp.lang.forth | 0 | 06-10-2009 12:34 PM |
| Re: Copying objects and multiple inheritance | Brian Allen Vanderburg II | Newsgroup comp.lang.python | 0 | 06-03-2009 01:20 PM |