473,395 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Virtual functrions and inheritance--hard to phrase question

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
Jul 22 '05 #1
2 969
"Squeamizh" <ad**@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
Jul 22 '05 #2
On Tue, 08 Jun 2004 03:01:23 GMT, "Victor Bazarov"
<v.********@comAcast.net> wrote:
"Squeamizh" <ad**@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.

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: heted7 | last post by:
Hi, Most of the books on C++ say something like this: "A virtual destructor should be defined if the class contains at least one virtual member function." My question is: why is it only for...
2
by: Flip | last post by:
In java, the default for methods is override. In c# that is not the case. This talks about two classes, the base class and the overriding class. What happens when you have a third class in the...
1
by: relient | last post by:
I'm learning about the virtual table in association with virtual methods. I got most of the logic and understanding down (I believe) for when you use 'override' and no 'override' or no 'new'...
16
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
7
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from...
2
by: sven.bauer | last post by:
Hi, I have a question following up the following slightly older posting: http://groups.google.de/group/comp.lang.c++/browse_thread/thread/40e52371e89806ae/52a3a6551f84d38b class Base {...
4
by: David Zha0 | last post by:
Hi, "when we call a virtual method, the runtime will check the instance who called the method and then choose the suitable override method, this may causes the performance drop down", is this...
7
by: sam_cit | last post by:
Hi Everyone, I wanted to know as to what is the exact difference between a virtual function and a pure virtual function? Thanks in advance!!!
0
by: JWcode | last post by:
Hello, I am trying to do paging and Virtual Earth on the same page. I want to have the ten results on each page mapped with Virtual Earth. I can't figure out how to go through the results a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.