Re: Virtual functrions and inheritance--hard to phrase question
From: Squeamizh (adsf_at_adf.adsf)
Date: 06/08/04
- Next message: Jeff Relf: "Linux on a hand-held."
- Previous message: Buster: "Re: [OT] Why code completion and early error checking are needed"
- In reply to: Victor Bazarov: "Re: Virtual functrions and inheritance--hard to phrase question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 08 Jun 2004 05:42:58 GMT
On Tue, 08 Jun 2004 03:01:23 GMT, "Victor Bazarov"
<v.Abazarov@comAcast.net> wrote:
>"Squeamizh" <adsf@adf.adsf> wrote...
>> I'm having an issue where my compiler is not letting me do something
>> that I think I should be able to do. I'm finding it very difficult to
>> express my problem with a description, so I'm just going to give an
>> example of something similar to what I'm doing...I hope it's
>> understandable. Here's the situation:
>>
>> class Alpha {
>> public:
>> virtual void translate(void) = 0;
>> ...
>> };
>>
>> class Beta : public Alpha {
>> public:
>> void translate(void);
>> ...
>> };
>>
>>
>> class TranslatableInterface {
>> public:
>> virtual void translate(void) = 0;
>> };
>>
>> class Gamma : public Beta, public TranslatableInterface {
>> ... //translate is not overridden here.
>> };
>>
>>
>>
>> But if I try to instantiate a Gamma object it tells me that Gamma is
>> an abstract class due its reference to the undefined virtual
>> translate function. Do I really have to create another translate
>> function within Gamma that just calls Beta::translate() ? God I hope
>> not, that would be annoying.
>
>You will have to. There are two base classes in Gamma, and one of
>them is abstract. That makes Gamma abstract until you define the
>final overrider for 'TranslatableInterface::translate'.
>
>What I fail to understand, really, is why you inherited Beta from
>Alpha and not from TranslatableInterface. If you did, you'd have
>no problem whatsoever:
>
> struct TranslatableInterface {
> virtual void translate() = 0;
> };
>
> struct Beta : virtual TranslatableInterface {
> void translate() {}
> };
>
> struct Gamma : Beta, virtual TranslatableInterface { };
>
> int main()
> {
> G g;
> A &a = g;
> a.translate();
> }
>
>Victor
Thanks.
I intentionally made the example simple, but now that I look at it I
can see why it seems like an irrational thing to do. Basically, Alpha
can do more than translate, and TranslateableInterface also has more
functionality than just to translate (It's definitely not called
'TranslatableInterface'), but I need the ability to translate Gamma
via either type of reference. I incorrectly assumed that, since Gamma
does inherit a translate function somewhere down the line, that that
should take care of any virtual void translate()s that it has.
- Next message: Jeff Relf: "Linux on a hand-held."
- Previous message: Buster: "Re: [OT] Why code completion and early error checking are needed"
- In reply to: Victor Bazarov: "Re: Virtual functrions and inheritance--hard to phrase question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|