From: beemaster on
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! ]

From: Goran on
On Dec 10, 12:02 am, beemaster <beemast...(a)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! ]

From: Martin B. on
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! ]

From: Bart van Ingen Schenau on
On Dec 10, 12:02 am, beemaster <beemast...(a)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! ]

From: Pete Becker on
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! ]