C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

multiple interface inheritance

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
shenry
Guest





PostPosted: Wed Dec 28, 2005 10:10 am    Post subject: multiple interface inheritance Reply with quote



Hi,

I have a question about multiple interface inheritance.
There are two interfaces: IA and IB (which derived from IA).

interface IA
{
.... // pure virtual functions
} ;

interface IB : public IA
{
.... // pure virtual functions
};

I also have an implementation for IA (class A):
class A : public IA
{
... // implementation from IA
};

I wish to implement IB (class B) as follows:
class B : public IB , public A
{
... // ONLY implementation from IB
};

How can I do that in a nice and clean way?
I recall a method known as alpha refinement, is it the best way to
achieve my goal?

Thanks in advance,
-- Henry


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Luke Meyers
Guest





PostPosted: Wed Dec 28, 2005 12:28 pm    Post subject: Re: multiple interface inheritance Reply with quote



Well, first of all, this isn't Java -- "interface" is not a C++
keyword. You should use "class" throughout (or struct, which defaults
everything to public).

Apart from that, I find your question a bit unclear. Purely abstract
classes provide no implementation, so it's inaccurate to refer to
"implementation from IA/IB." I presume you mean "implementation for
IA/IB's methods." That being the case, it's not obvious what you want
that isn't provided by the code you've laid out. If you want to
implement IA in A, and IB in B, then that's what you've got.

Perhaps what you mean is that you want to only expose IB, not A/IA,
from B? In that case, use private or protected inheritance.

If this doesn't answer your question, can you please clarify your
intent?

Luke


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze
Guest





PostPosted: Thu Dec 29, 2005 12:12 pm    Post subject: Re: multiple interface inheritance Reply with quote



shenry wrote:

Quote:
I have a question about multiple interface inheritance. There
are two interfaces: IA and IB (which derived from IA).

interface IA
{
... // pure virtual functions
} ;

interface IB : public IA
{
... // pure virtual functions
};

First, there's no keyword "interface" in C++. I suppose here
that you are thinking in terms of IDL, or some abstract
specification language.

In C++, with the exception of code generated by Corba, it's
relatively rare for a class to consist of only pure virtual
functions. An "interface" is normally implemented by means of
non virtual public functions, which wrap the call to the pure
virtual private functions in code which verifies the pre- and
post-conditions. Typically, something like:

class IA
{
public:
virtual ~IA() {}

int calculateSomething( int in1, int in2 )
{
do {
assert( preconditions ) ;
int result
= doCalculateSomething( in1, in2 ) ;
assert( postconditions ) ;
return result ;
} catch ( ... ) {
assert( error_postconditions ) ;
throw ;
}
}

private:
virtual int doCalculateSomething( int in1, int in2 ) =
0 ;
} ;

In some cases, you might also want to add something like:
IA const* old = clone() ;
before calling the virtual function, in order to have the
previous value available for post-condition checks.

Quote:
I also have an implementation for IA (class A):
class A : public IA
{
.. // implementation from IA
};

I wish to implement IB (class B) as follows:
class B : public IB , public A
{
.. // ONLY implementation from IB
};

How can I do that in a nice and clean way?

class IB : public virtual IA
{
// ...
} ;

class A : public virtual IA
{
// ...
} ;

class B : public virtual IB, private /*(virtual?)*/ A
{
} ;

For maximum flexibility, derivation from an interface should be
virtual. In practice, it is often fairly obvious when such
cases may reasonably occur, and when not -- if there is any
doubt, however, virtual doesn't hurt, and leaves the door open
to this sort of possibility.

It is much, much rarer that inheritance of implementation needs
to be virtual. About the only time I've seen such cases has
been with mixin's, and of course, there it is obvious from the
beginning that the virtual is needed.

Quote:
I recall a method known as alpha refinement, is it the best
way to achieve my goal?

Never heard of it, and Google doesn't seem to come up with any
relevant hits either.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Maxim Yegorushkin
Guest





PostPosted: Sun Jan 01, 2006 11:23 am    Post subject: Re: multiple interface inheritance Reply with quote


shenry wrote:
Quote:
Hi,

I have a question about multiple interface inheritance.
There are two interfaces: IA and IB (which derived from IA).

interface IA
{
... // pure virtual functions
} ;

interface IB : public IA
{
... // pure virtual functions
};

I also have an implementation for IA (class A):
class A : public IA
{
.. // implementation from IA
};

I wish to implement IB (class B) as follows:
class B : public IB , public A
{
.. // ONLY implementation from IB
};

How can I do that in a nice and clean way?
I recall a method known as alpha refinement, is it the best way to
achieve my goal?

The method is called chaining.

struct one
{
virtual void foo() = 0;
};

struct two : one
{
virtual void bar() = 0;
};

template<class base>
struct one_impl : base
{
void foo() { /*...*/ }
};

template<class base>
struct two_impl : base
{
void bar() { /*...*/ }
};

typedef two_impl<one_impl concrete;
concrete c;


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.