calling interface functions with the same name

Started by
7 comments, last by Genjix 18 years, 10 months ago

class A
{
    public:
	virtual void D(int j) = 0;
}

class B : virtual public A
{
    public:
	virtual void D(int j);
}

class C : virtual public A
{
    public:
	virtual void D();
}

class E : public B , public C
{
	// ...
}


is there any way I can get C::D() to call B::D(int j) when in E, without changing the function names so they are different?
Advertisement
Have you tried "B::D();" or "C::D();"?

Just off the top of my head...
I'm pretty sure you can't, then it'ld be impossible to make an object of the type C because it can't call "void B::D(int j)". If C needs to call a function inside B, C should inherit from B.

EDIT: Well know that I think about it I don't really know what you mean, does you mean this:
class C : public A{    public:	virtual void D()        {            D(42);        }}


or this:
class E : public C,public B{    public:	virtual void D()        {            D(42);        }}


The second example would be possible, but the first won't.

[Edited by - CTar on June 7, 2005 2:42:09 PM]
Example:
class A{   public:	virtual void D(int j) = 0;};class B : virtual public A{   public:	virtual void D(int j)   {      printf("B::D Called\r\n");     };};class C : virtual public A{   public:	virtual void D()   {      printf("C::D Called\r\n");   }};class E : public B , public C{public:   void test()   {      B::D(1);      C::D();   }};/// .. elsewhereE e;e.test();


output:
B::D Called
C::D Called
You may also want to look at virtual inheritance for this situation.
sorry for my horrible minimal wording.

basically i wanted to from within E call C::D() which calls the overriden A::D(int) or B::D(int).

but as some of you have said... this isn't possible unless I change the name of A::D(int)?
Quote:Original post by Genjix
sorry for my horrible minimal wording.

basically i wanted to from within E call C::D() which calls the overriden A::D(int) or B::D(int).

but as some of you have said... this isn't possible unless I change the name of A::D(int)?


Ah.. you want C::D to call B::D.. nope, can't be done.. or it would involve some serious hacks.
It can be hacked. I think this might work:
void C :: D ( ){    ((B*)this)->B::D( );}

~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
I take it this is another 'deliberate' feature of C++ set by the standard? Is there any reason this is so?

This topic is closed to new replies.

Advertisement