 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Gav Wood Guest
|
Posted: Mon Aug 16, 2004 3:01 pm Post subject: specialising through inheritance |
|
|
i wonder if it is possible to "implement" virtual methods through
inheritance only. consider the following code:
class A
{
protected:
virtual int bar() = 0;
public:
int foo() { return bar(); }
};
class B1 { virtual int bar() { return 1; } };
class B2 { virtual int bar() { return 2; } };
class C1: virtual public A, virtual protected B1 {} c1;
class C2: virtual public A, virtual protected B2 {} c2;
int main()
{
int x;
x = c1.foo(); // Expect 1
x = c2.foo(); // Expect 2
}
this apparently doesn't compile as it stands - is there some way to achieve
this without resorting to setting up some sort of relay code in C1/C2?
bish bosh,
gav
--
York
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Pavel Vozenilek Guest
|
Posted: Mon Aug 16, 2004 8:06 pm Post subject: Re: specialising through inheritance |
|
|
"Gav Wood" wrote:
| Quote: | i wonder if it is possible to "implement" virtual methods through
inheritance only. consider the following code:
[snip code] |
Maybe you look for mixing technique:
http://cpptips.hyperformix.com/cpptips/mixins
/Pavel
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dag Viken Guest
|
Posted: Mon Aug 16, 2004 8:06 pm Post subject: Re: specialising through inheritance |
|
|
"Gav Wood" <gav (AT) cs (DOT) york.ac.uk> wrote
| Quote: | i wonder if it is possible to "implement" virtual methods through
inheritance only. consider the following code:
class A
{
protected:
virtual int bar() = 0;
public:
int foo() { return bar(); }
};
class B1 { virtual int bar() { return 1; } };
class B2 { virtual int bar() { return 2; } };
class C1: virtual public A, virtual protected B1 {} c1;
class C2: virtual public A, virtual protected B2 {} c2;
int main()
{
int x;
x = c1.foo(); // Expect 1
x = c2.foo(); // Expect 2
}
this apparently doesn't compile as it stands - is there some way to
achieve
this without resorting to setting up some sort of relay code in C1/C2?
The C1/C2 classes have two base classes, each with their own definition of |
bar() and since A::bar() has not been instantiated your code will not
compile. The virtual attribute for the base classes of C1/C2 has no effect.
Instead A and B1/B2 must derive virtually from the same base class, maybe
like this:
class Base
{
protected:
virtual int bar() = 0;
};
class A: virtual public Base
{
public:
int foo() { return bar(); }
};
class B1: virtual public Base { int bar() { return 1; } };
class B2: virtual public Base { int bar() { return 2; } };
class C1: public A, protected B1 {} c1;
class C2: public A, protected B2 {} c2;
int main()
{
int x;
x = c1.foo(); // Expect 1
x = c2.foo(); // Expect 2
}
Dag
| Quote: | bish bosh,
gav
--
York
|
[ 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: Tue Aug 17, 2004 7:00 pm Post subject: Re: specialising through inheritance |
|
|
Gav Wood <gav (AT) cs (DOT) york.ac.uk> wrote:
| Quote: | i wonder if it is possible to "implement" virtual methods through
inheritance only. consider the following code:
class A
{
protected:
virtual int bar() = 0;
public:
int foo() { return bar(); }
};
class B1 { virtual int bar() { return 1; } };
class B2 { virtual int bar() { return 2; } };
class C1: virtual public A, virtual protected B1 {} c1;
class C2: virtual public A, virtual protected B2 {} c2;
int main()
{
int x;
x = c1.foo(); // Expect 1
x = c2.foo(); // Expect 2
}
this apparently doesn't compile as it stands - is there some way to achieve
this without resorting to setting up some sort of relay code in C1/C2?
|
If templates as base classes are ok for you, you might use something like:
template<class T, int N>
struct B : T
{
int bar() { return N; }
};
struct C1 : B<A, 1> {} c1;
struct C2 : B<A, 2> {} c2;
--
Maxim Yegorushkin
[ 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
|
|