473,388 Members | 1,423 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,388 software developers and data experts.

How do i correctly implement: two inherited (multiple), same-named,pure virtual methods inside a derived class?



I'm inheriting from multiple abstract base classes (2 to be exact).
In the inherited class I want to know if I can implement two pure
virtual methods (one from each base class) having the same name?

An example is worth a thousand words (Note: I didn't check syntax but
you will get this gist of it), so here it is ....

class Base1
{
virtual void action() = 0;
};

class Base2
{
virtual void action() = 0;
};

class Derived : public Base1, public Base2
{
/*How the heck do I implement both inherited "action" methods? I filled
in an idea here that I'm working on, but cannot seem to get it to
compile ...
*/
Base1::action() { /* do stuff */; } // does not work
Base2::action() { /* do stuff */; } // ditto
};

???

Thanks.
--John
Jul 22 '05 #1
6 2000
John wrote:

class Base1
{
virtual void action() = 0;
};

class Base2
{
virtual void action() = 0;
};

class Derived : public Base1, public Base2
{
void action() { /* do stuff */; }
};

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #2
John wrote:


I'm inheriting from multiple abstract base classes (2 to be exact).
In the inherited class I want to know if I can implement two pure
virtual methods (one from each base class) having the same name?

An example is worth a thousand words (Note: I didn't check syntax but
you will get this gist of it), so here it is ....

class Base1
{
virtual void action() = 0;
};

class Base2
{
virtual void action() = 0;
};

class Derived : public Base1, public Base2
{
/*How the heck do I implement both inherited "action" methods?

Just the same as you do always:

virtual void action() { /* do stuff */ }
I filled in an idea here that I'm working on, but cannot seem to get
it to compile ...
*/
Base1::action() { /* do stuff */; } // does not work
Base2::action() { /* do stuff */; } // ditto
};

???


Jul 22 '05 #3
Pete Becker <pe********@acm.org> wrote:
John wrote:

class Base1
{
virtual void action() = 0;
};

class Base2
{
virtual void action() = 0;
};

class Derived : public Base1, public Base2
{


void action() { /* do stuff */; }
};


Of course, this implies that the specification for both 'action()' methods
is identical or at least similar. This is not necessarily a given (although,
admittedly, likely for a function returning nothing and taking no parameter).
If both bases have different requirements on the implementation of the
function and/or a different return type, you have to use two auxiliary base
classes, each implementing one of the functions. I don't think that C++ has
a mechanism like e.g. C# which allows specification of the base class when
overriding a virtual function.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
Jul 22 '05 #4
Dietmar Kuehl posted:
I don't think that C++ has a mechanism like e.g. C# which
allows specification of the base class when overriding a virtual
function.

class Base1 ...

class Base2 ...

class Derived : public Base1, public Base2 ...
int main()
{
Derived o;

o.Blah;

o.Base1::Blah();

o.Base2::Blah();
}
Or:

void Derived::Blah()
{
this->Base1::Blah();
this->Base2::Blah();
}
-JKop
Jul 22 '05 #5
JKop wrote:
Dietmar Kuehl posted:
I don't think that C++ has a mechanism like e.g. C# which
allows specification of the base class when overriding a virtual
function.

Note my statement: "... when *overriding* a virtual function." I'm aware and
didn't dispute the fact that you can use specification of a base class when
*calling* a virtual function (well, actually, any function).
class Base1 ...
class Base2 ...
class Derived : public Base1, public Base2 ...
int main()
{
Derived o;
o.Blah;
o.Base1::Blah();
o.Base2::Blah();
}
This is, however, calling a virtual function.
void Derived::Blah()
{
this->Base1::Blah();
this->Base2::Blah();
}


OK, this may be overriding a virtual function but you still cannot qualify
which virtual function is overridden which was what I was referring to,
e.g.:

struct Base1 { virtual int f() = 0; };
struct Base2 { virtual double f() = 0; };
struct Derived1: Base1, Base2 {
// how to define 'f()' here?
};
struct Aux1: Base1 { int f() { return 17; } };
struct Aux2: Base2 { double f() { return 3.14; } };
struct Derived2: Aux1, Aux2 {
// no need to define 'f()' here...
};
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
Jul 22 '05 #6
* Dietmar Kuehl:

I don't think that C++ has
a mechanism like e.g. C# which allows specification of the base class when
overriding a virtual function.


Right. Visual C++ has that as an extension. With some mysterious quirks...

--
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?
Jul 22 '05 #7

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

Similar topics

2
by: Terrance | last post by:
Hello all I have a question I'm hoping someone can shed some light on. I have 2 user Inherited Controls on a form. When I click 1 inherited control I would like to change the text of the other...
6
by: Andreas | last post by:
Hello list, what about uniqueness of inherited primary keys ? eg you have : create table objects ( id int4, date_created timestamp(0), primary key (id)
3
by: Dave Munger | last post by:
Apologies if this is a known issue, but I couldn't seem to turn it up with google or on Technet. I am using Visual C# 2005 Express, Beta 2. I am learning C# and have come to the point in the...
1
by: RRiness | last post by:
I'm trying to use a template form I created. When I add an inherited form to my project, I get the error: Object reference not set to an instance of an object. when the inherited form is...
0
by: elise | last post by:
I want to put my navigating code for the buttons in a inherited form because always the same code almost The currency manager changes everytime but how to implement this in cod e I am a newbie...
2
by: Carlos Durán Urenda | last post by:
Hi I need to know in that moment a tablestyles joins to my inherited DataGrid control Tnks
1
by: MariusI | last post by:
I have some business objects which overrides Equals to provide syntax equality instead of just reference equality. Overriding equals gives me a warning that i must override GetHashcode(). Msdn says...
8
by: Alec MacLean | last post by:
Hi, I'm using the DAAB Ent Lib (Jan 2006) for .NET 2.0, with VS 2005 Pro. My project is a Web app project (using the WAP add in). Background: I'm creating a survey system for our company, for...
0
by: SM | last post by:
Hi, #1 I did some changes in my inherited form (change some properties of my components), now I want to rollback all changes made in inherited form and return to the same state as the...
0
by: Paul W | last post by:
Hello everyone, My problem is with an inherited Form. The base Form has a few controls on it that are anchored such that they move as the Form is resized. While in the designer, the inherited...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.