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

J

John

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
 
P

Pete Becker

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

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

class Derived : public Base1, public Base2
{

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

Rolf Magnus

John said:
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 */ }
 
D

Dietmar Kuehl

Pete Becker said:
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.
 
J

JKop

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
 
D

Dietmar Kuehl

JKop said:
Dietmar Kuehl posted:

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...
};
 
A

Alf P. Steinbach

* 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...
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top