Re: Simple OO design issue
On Fri, 01 Jun 2007 00:55:56 -0700, obrianpatrick@gmail.com wrote:
Hi,
I am having the following design
problem:
I have two interfaces X and Y. Y is derived from X as the following:
__interface X
{
public:
virtual void func1(int) = 0;
};
I think that __interface would be more useful in a context of defining
COM interfaces...
For plain C++, I prefer:
class X
{
public:
// Don't forget the virtual destructor!
virtual ~X() {}
virtual void func1( int ) = 0;
};
And similar for Y interface:
class Y : public X
{
public:
virtual ~Y() {}
virtual void func2(int) = 0;
};
(I don't know if __interface also defines a virtual destructor, which
is important when you delete C++ objects that are in a hierarchy
chain.)
class YImpl: public Y
will not allow me to use the func1() implementation of XImpl. I have
to rewrite the same function in YImpl then.
You could code like this:
class YImpl : public Y
{
public:
void func1(int i)
{
// Route function call to member object
mX.func1(i);
}
void func2(int i)
{
cout << "YImpl::func2 - i = " << i << endl;
}
private:
XImpl mX;
};
In that code, I just created a member instance of XImpl into YImpl,
and route the func1 call to XImpl data member.
On the other hand inheriting YImpl multiply from XImpl and Y as
as
class YImpl: public XImpl, public Y
is not a good idea as X interface is included twice (and the VC++
complier is not allowing it either, rightly complaining that func1 is
ambiguous).
In this case, you might do:
class YImpl : public XImpl, public Y
{
public:
void func1(int i)
{
// Note XImpl:: scope specifier
XImpl::func1(i);
}
void func2(int i)
{
cout << "YImpl::func2 - i = " << i << endl;
}
};
MrAsm