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

REQ: how to override 2 virtual functions with the same name and same parameter

I have interface A and interface B decalared as follow:
class A
{
public:
virtual void OnError(std::string reson) = 0;
};

class B
{
public:
virtual void OnError(std::string reson) = 0;
};

And I want to construct a class c inherit both from A and B;

class C: public A, public B
{
public:
void OnError(std::string reson); // for A
void OnError(std::string reson); // for B
};

any one can help me how to implement 2 OnError, one for interface A and
one for Interface B

thanx for ur help

Feb 6 '06 #1
5 1656
zz*******@gmail.com wrote:
I have interface A and interface B decalared as follow:
class A
{
public:
virtual void OnError(std::string reson) = 0;
};

class B
{
public:
virtual void OnError(std::string reson) = 0;
};

And I want to construct a class c inherit both from A and B;

class C: public A, public B
{
public:
void OnError(std::string reson); // for A
void OnError(std::string reson); // for B
};

any one can help me how to implement 2 OnError, one for interface A and
one for Interface B


You can't. C++ forbids the definition of two different functions in
the same namespace with the same signature. You can't overload based
on which parent you've got in mind. How would client code distinguish
between them (when not using a pointer-to-base)?

Luke

Feb 6 '06 #2
zz*******@gmail.com wrote:
I have interface A and interface B decalared as follow:
class A
{
public:
virtual void OnError(std::string reson) = 0;
};

class B
{
public:
virtual void OnError(std::string reson) = 0;
};

And I want to construct a class c inherit both from A and B;

class C: public A, public B
{
public:
void OnError(std::string reson); // for A
void OnError(std::string reson); // for B
};

any one can help me how to implement 2 OnError, one for interface A and
one for Interface B

thanx for ur help


You can use 2 different functions with the same signature only in 2
different namespaces. I do not think you could have the above code
since how would the user differentiate between which OnError() do you
want to use.

Feb 6 '06 #3
* zz*******@gmail.com:
I have interface A and interface B decalared as follow:
class A
{
public:
virtual void OnError(std::string reson) = 0;
};

class B
{
public:
virtual void OnError(std::string reson) = 0;
};

And I want to construct a class c inherit both from A and B;

class C: public A, public B
{
public:
void OnError(std::string reson); // for A
void OnError(std::string reson); // for B
};

any one can help me how to implement 2 OnError, one for interface A and
one for Interface B


How should the caller indicate which one to call? That would be
impossible. Hence you need to do something else, such as using
composition instead of inheritance:

class C;

class CA: public A { friend class C; ... };
class CB: public B { friend class C; ... };

class C
{
public:
CA a;
CB b;
};

Here the purpose of class C is simply to coordinate the CA and CB
instances and provide code that is common to both. Typically you'd pass
a reference to the C object to the CA and the CB constructors.

Btw., that std::string argument should really be passed as a std::string
const&, and inheritance of the interfaces should probably be virtual.

Hth.,

- Alf

--
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?
Feb 6 '06 #4
zz*******@gmail.com wrote:
I have interface A and interface B decalared as follow:
class A
{
public:
virtual void OnError(std::string reson) = 0;
};

class B
{
public:
virtual void OnError(std::string reson) = 0;
};

And I want to construct a class c inherit both from A and B;

class C: public A, public B
{
public:
void OnError(std::string reson); // for A
void OnError(std::string reson); // for B
};

any one can help me how to implement 2 OnError, one for interface A and
one for Interface B


Like this:

class A
{
public:
virtual void OnError(std::string reson) = 0;
};

class B
{
public:
virtual void OnError(std::string reson) = 0;
};

class A_proxy
: A
{
public:
virtual void OnErrorA(std::string reson) = 0;
virtual void OnError(std::string reson)
{
OnErrorA( reason );
}
};

class B_Proxy
: B
{
public:
virtual void OnErrorB(std::string reson) = 0;
virtual void OnError(std::string reson)
{
OnErrorB( reason );
}
};
class C: public A_Proxy, public B_Proxy
{
public:
void OnErrorA(std::string reson); // for A
void OnErrorB(std::string reson); // for B
};
Feb 6 '06 #5
* Gianni Mariani:
zz*******@gmail.com wrote:
I have interface A and interface B decalared as follow:
class A
{
public:
virtual void OnError(std::string reson) = 0;
};

class B
{
public:
virtual void OnError(std::string reson) = 0;
};

And I want to construct a class c inherit both from A and B;

class C: public A, public B
{
public:
void OnError(std::string reson); // for A
void OnError(std::string reson); // for B
};

any one can help me how to implement 2 OnError, one for interface A and
one for Interface B


Like this:

class A
{
public:
virtual void OnError(std::string reson) = 0;
};

class B
{
public:
virtual void OnError(std::string reson) = 0;
};

class A_proxy
: A
{
public:
virtual void OnErrorA(std::string reson) = 0;
virtual void OnError(std::string reson)
{
OnErrorA( reason );
}
};

class B_Proxy
: B
{
public:
virtual void OnErrorB(std::string reson) = 0;
virtual void OnError(std::string reson)
{
OnErrorB( reason );
}
};

class C: public A_Proxy, public B_Proxy
{
public:
void OnErrorA(std::string reson); // for A
void OnErrorB(std::string reson); // for B
};


I'll assume that you mean public inheritance all the way since A and B
are interfaces, not classes to inherit implementations from.

The above then works technically but there is a design problem, namely
that class C says it "is a" A and "is a" B, but if you pass a C instance
to template code that assumes A (or B) it clearly isn't, for calls to
OnError in the template code will then give compile time errors.

That can be technically solved by casting in the code that passes in the
C instance.

Essentially that casting means recognizing in the client code that C
isn't really an A or B, but a composition expressed using C++
inheritance: that class C itself doesn't have e.g. OnError.

For that reason I think actual C++ level composition is just as good a
choice. The C++ code then corresponds directly to the design, and makes
the design explicit, not something to be figured out. However, it's a
little more to write to implement it that way, so it's not clear-cut.

--
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?
Feb 6 '06 #6

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

Similar topics

3
by: Daniel Graifer | last post by:
Why doesn't c++ support virtual data? It supports class data of type pointer to function (that's what virtual functions really are). In terms of implementation, why can't I have other types of...
22
by: Ruben Van Havermaet | last post by:
Hi, I have a problem using member functions in derived classes that override virtual member functions of base classes. The following pieces of (simplified) code don't work. Can anybody give...
8
by: Floogle | last post by:
how do i create a virtual == operator. I've tried the following but it's incorrect... class Interface { ... public: virtual bool operator==(const Interface& rhs)const=0;
14
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
5
by: Mark Broadbent | last post by:
Oh yes its that chestnut again! Ive gone over the following (http://www.yoda.arachsys.com/csharp/faq/ -thanks Jon!) again regarding this subject and performed a few of my own tests. I have two...
2
by: Ben Voigt | last post by:
I have a function that is called for several distinct reasons. It is conceivable that a subclass would want to handle those cases differently. Therefore, what I want to do is have multiple v-table...
2
by: tony | last post by:
Hi! A derived class can override a method in the base class it inherits for, and even my dog knows that. More incredibly, I know and understand it too. But, can a (of course virtual in this...
7
by: asdf | last post by:
They looks so similar. Anybody would like to tell me their differences? Thanks a lot.
2
by: dolphin | last post by:
Hi everyone . I am confused about the different between override overload and hide. May be I have a wrong opinion, I always think that hide is very similar with override. Both of them call the...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.