 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Adam Guest
|
Posted: Tue May 23, 2006 10:21 pm Post subject: Multiply inherit from classes with conflicting function name |
|
|
I have an unfortunate case where a single class wants to derive from two
existing classes:
struct A { virtual long fun() = 0; };
struct B { virtual bool fun() = 0; };
struct Unfortunate : public A, public B { ??? };
Is it possible to fill in the ??? here with legal code?
I need two different function bodies; A::fun and B::fun do unrelated
things.
More or less the same question with a twist: if A::fun and B::fun both
returned the same type, would it be possible to implement two functions
in C such that
C().A::fun()
and
C().B::fun()
would execute two different functions?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Wed May 24, 2006 4:21 pm Post subject: Multi-posting clc++ and clc++m (was: Multiply inherit from c |
|
|
* Adam, in clc++m:
| Quote: | I have an unfortunate case where a single class wants to derive from two
existing classes:
struct A { virtual long fun() = 0; };
struct B { virtual bool fun() = 0; };
struct Unfortunate : public A, public B { ??? };
|
I already answered this one (the exact same message text), apparently to
Adam's satisfaction, in [clc++].
Could people please /stop/ multi-posting (at least not without noting in
the message that it's multi-posted)?
IMO it's now become a nuisance.
Note: this message has been /cross-posted/ to clc++ and clc++m.
For the difference between cross-posting and multi-posting, see Google.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Wed May 24, 2006 4:21 pm Post subject: Re: Multiply inherit from classes with conflicting function |
|
|
In article <15-dnUdwgJlQB-_ZRVn-pQ (AT) speakeasy (DOT) net>, Adam
<usenet-filter (AT) aulick (DOT) net> wrote:
| Quote: | I have an unfortunate case where a single class wants to derive from two
existing classes:
struct A { virtual long fun() = 0; };
struct B { virtual bool fun() = 0; };
struct Unfortunate : public A, public B { ??? };
give them new names by adding mid_A,mid_B to the inheritance? |
struct mid_A:A {virtual long fun();long fun_A(){return fun();}};
struct mid_B:B {virtual bool fun(); bool fun_B(){return fun();}};
struct Unfortunate:mid_A,Mid_B {}
if these are really structs the above fun()'s can access
Unfortunates' data via a static_cast<Unfortunate *>(this);
if Unfortunate is a class then also make mid_A,mid_B friends of
Unfortunate.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Guest
|
Posted: Wed May 24, 2006 4:21 pm Post subject: Re: Multiply inherit from classes with conflicting function |
|
|
Adam wrote:
| Quote: | I have an unfortunate case where a single class wants to derive from two
existing classes:
struct A { virtual long fun() = 0; };
struct B { virtual bool fun() = 0; };
struct Unfortunate : public A, public B { ??? };
Is it possible to fill in the ??? here with legal code?
|
You can introduce trivial intermediate classes to "rename" the
functions.
| Quote: | I need two different function bodies; A::fun and B::fun do unrelated
things.
More or less the same question with a twist: if A::fun and B::fun both
returned the same type, would it be possible to implement two functions
in C such that
C().A::fun()
and
C().B::fun()
would execute two different functions?
|
The intermediate classes would still work, but with a different syntax:
C().A_fun();
Here is a complete test:
#include <iostream>
struct A { virtual long fun() = 0; };
struct B { virtual bool fun() = 0; };
struct A_impl : public A
{
virtual long A_fun() = 0;
virtual long fun()
{
return A_fun();
}
};
struct B_impl : public B
{
virtual bool B_fun() = 0;
virtual bool fun()
{
return B_fun();
}
};
struct Unfortunate : public A_impl, public B_impl
{
virtual long A_fun()
{
std::cout << "long\n";
return 0;
}
virtual bool B_fun()
{
std::cout << "bool\n";
return false;
}
};
int main()
{
Unfortunate u;
// Ambiguous to call fun()
u.A_fun();
u.B_fun();
// No ambiguity through the base interfaces
A & a = u;
a.fun();
B & b = u;
b.fun();
}
Ali
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Nicola Musatti Guest
|
Posted: Thu May 25, 2006 2:21 pm Post subject: Re: Multiply inherit from classes with conflicting function |
|
|
Adam wrote:
| Quote: | I have an unfortunate case where a single class wants to derive
from two
existing classes:
struct A { virtual long fun() = 0; };
struct B { virtual bool fun() = 0; };
struct Unfortunate : public A, public B { ??? };
Is it possible to fill in the ??? here with legal code?
I need two different function bodies; A::fun and B::fun do unrelated
things.
|
The usual technique is the following:
struct AdaptA : A {
virtual long funA() = 0;
long fun() { return funA(); }
};
struct AdaptB : B {
virtual long funB() = 0;
long fun() { return funB(); }
};
struct Unfortunate : AdaptA, AdaptB {
long funA() { return 42L }
bool funB() { return false; }
};
| Quote: | More or less the same question with a twist: if A::fun and B::fun
both
returned the same type, would it be possible to implement two
functions
in C such that
C().A::fun()
and
C().B::fun()
would execute two different functions?
|
I don't think the return type makes any difference.
Cheers,
Nicola Musatti
[ 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
|
Posted: Thu May 25, 2006 2:21 pm Post subject: Re: Multiply inherit from classes with conflicting function |
|
|
Adam wrote:
| Quote: | I have an unfortunate case where a single class wants to
derive from two existing classes:
struct A { virtual long fun() = 0; };
struct B { virtual bool fun() = 0; };
struct Unfortunate : public A, public B { ??? };
Is it possible to fill in the ??? here with legal code?
|
Legal code, certainly. But nothing that would stop the class
from being abstract -- there's no way you can override fun().
| Quote: | I need two different function bodies; A::fun and B::fun do
unrelated things.
|
The classical solution is to create intermediate classes:
struct Abis { virtual long funA() = 0 ;
virtual long fun() { return funA() ; } } ;
struct Bbis { virtual bool funB() = 0 ;
virtual bool fun() { return funB() ; } } ;
struct Unfortunate : A, B
{
virtual long funA() { ... }
virtual long funB() { ... }
} ;
| Quote: | More or less the same question with a twist: if A::fun and
B::fun both returned the same type, would it be possible to
implement two functions in C such that
C().A::fun()
and
C().B::fun()
would execute two different functions?
|
Same solution as above.
--
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
|
Posted: Thu May 25, 2006 2:21 pm Post subject: Re: Multiply inherit from classes with conflicting function |
|
|
Adam wrote:
| Quote: | I have an unfortunate case where a single class wants to derive
from two
existing classes:
struct A { virtual long fun() = 0; };
struct B { virtual bool fun() = 0; };
struct Unfortunate : public A, public B { ??? };
Is it possible to fill in the ??? here with legal code?
I need two different function bodies; A::fun and B::fun do unrelated
things.
|
Yes, you need an intermediate class here.
struct A_tag {} const a_tag = {};
struct B_tag {} const b_tag = {};
// zero overhead shim
template<class derived>
struct A_shim : A
{
long fun() { return static_cast<derived*>(this)->fun(a_tag); }
};
// virtual call overhead shim
struct B_shim : B
{
bool fun() { this->fun(b_tag); }
virtual bool fun(B_tag) = 0;
};
struct fortunate : A_shim<fortunate>, B_shim
{
long fun(A_tag);
bool fun(B_tag);
};
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Adam Aulick Guest
|
Posted: Fri May 26, 2006 7:58 am Post subject: Re: Multi-posting clc++ and clc++m |
|
|
Alf P. Steinbach wrote:
| Quote: | Could people please /stop/ multi-posting (at least not without
noting in
the message that it's multi-posted)?
IMO it's now become a nuisance.
|
For the record, posting to the moderated group was an accident in this
case. Embarassing since I've been impatient with multi-posters
elsewhere in the past, but there's karma for you. My apologies.
~Adam
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
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
|
|