473,796 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2015
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.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - 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.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - 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
1292
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 Inherited Control. Is there anyway to do this? For example, I have an ADD button which is inherited and a EXIT button which is inherited. The reason I made them inherited is because I want to use the function of these buttons in other forms and don't...
6
2502
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
4508
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 book I am reading, which covers form inheritance. Granted, the book is for Visual Studio 2003, but it directs me to use a menu item called "Add Inherited Form," available on the Project menu. This menu item, however, does not appear for me. I...
1
1576
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 displayed in the designer. Last week I used the same template and didn't have any trouble creating inherited forms. I can look at the new form in code view, and don't see any obvious errors. Help!
0
763
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 , but whilling to learn as quicj as i can but i can't figure this out Thxxxx'y
2
940
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
2205
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 this about implementing hashcode: (1)If two objects of the same type represent the same value, the hash function must return the same constant value for either object. (2)For the best performance, a hash function must generate a random...
8
2703
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 which invites will target selected personnel among our customers via email. Each email will provide a custom hyperlink for each respondent using a SQL generated GUID value in the querystring. The GUID will be checked for validity before the user...
0
1215
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 base form, I used to use Delphi and this powerful and stable environment include nice functionally we just right
0
1470
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 Form works perfectly but when I run the program the controls on the inherited Form are positioned based on the size of the base Form instead of the inherited Form. The controls move approriately as the Form is resized but they are never in the...
0
9527
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10453
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10172
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.