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