Virtual functrions and inheritance--hard to phrase question

S

Squeamizh

Hi,

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.

Thank you very much
 
V

Victor Bazarov

Squeamizh said:
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
 
S

Squeamizh

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top