|
Prev: Call for Papers: International Conference on Computer Science and Applications ICCSA 2007
Next: UML modeling of a class referencing itselfs
From: obrianpatrick on 1 Jun 2007 03:55 Hi, I am relatively new to object oriented programming and design. I am developing an application in VS 2005. 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; }; __interface Y: public X { public: virtual void func2(int) = 0; }; X interface is implemented in the class XImpl as follow: class XImpl: public X { public: void func1(int i) { cout << "In XImpl::func 1: i = " << i; } }; Now I want to have a class YImpl implementing func2() of Y interface. func1 implementation should remain the same as in XImpl. How should I define the class YImpl? Deriving it only from Y interface, as 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. 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). How should the class YImpl be defined and implemented? Please note that I can change the definition or implementation of XImpl but not the definition of X and Y interfaces as they have come from some other sources on which I have no control. Thanks in advance, Patrick O'Brian
From: Ian Collins on 1 Jun 2007 06:08 obrianpatrick(a)gmail.com wrote: > Hi, > > I am relatively new to object oriented programming and design. I am > developing an application in VS 2005. I am having the following design > problem: > > I have two interfaces X and Y. Y is derived from X as the following: > > __interface X Where does this come from, it ain't C++? -- Ian Collins.
From: obrianpatrick on 1 Jun 2007 06:33 Hi Stuart, Thanks a ton. How neat!! Patrick On Jun 1, 2:39 pm, Stuart Redmann <DerTop...(a)web.de> wrote: > obrianpatr...(a)gmail.com wrote: > > Hi, > > > I am relatively new to object oriented programming and design. I am > > developing an application in VS 2005. 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; > > }; > > > __interface Y: public X > > { > > public: > > virtual void func2(int) = 0; > > }; > > Common scenario. > > > X interface is implemented in the class XImpl as follow: > > > class XImpl: public X > > { > > public: > > void func1(int i) > > { > > cout << "In XImpl::func 1: i = " << i; > > } > > }; > > Okay. > > > > > > > Now I want to have a class YImpl implementing func2() of Y interface. > > func1 implementation should remain the same as in XImpl. How should I > > define the class YImpl? Deriving it only from Y interface, as > > > 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. > > > 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). > > > How should the class YImpl be defined and implemented? Please note > > that I can change the definition or implementation of XImpl but not > > the definition of X and Y interfaces as they have come from some other > > sources on which I have no control. > > You have stumbled over some implementation issues that come together with > interface programming (when using some component architecture). The standard > solution to your problem is to make the implementation class XImpl a template: > > template<class t_BaseInterface> > class XImpl : public t_BaseInterface > { > public: > void func1(int i) > { > // Implementation of func1 of interface X > } > > }; > > If you want to use XImpl for implementing, you have to use it like XImpl<X> (or > you can make X the default base interface for XImpl like template <class > t_BaseInterface = X>, than you can say XImpl<> with empty template parameter list). > For YImpl you have to say: > class YImpl : public XImpl<Y> > { > .... > > }; > > Now XImpl implements the func1 of interface Y, and YImpl needs only to implement > func2. > > Regards, > Stuart > > BTW: If you cross-post to several newsgroups, you should rather specify a > follow-up newsgroup, so that any replies to your post are accumulated in a > single newsgroup.- Hide quoted text - > > - Show quoted text -- Hide quoted text - > > - Show quoted text -
From: Branimir Maksimovic on 1 Jun 2007 08:01 On Jun 1, 12:08 pm, Ian Collins <ian-n...(a)hotmail.com> wrote: > obrianpatr...(a)gmail.com wrote: > > Hi, > > > I am relatively new to object oriented programming and design. I am > > developing an application in VS 2005. I am having the following design > > problem: > > > I have two interfaces X and Y. Y is derived from X as the following: > > > __interface X > > Where does this come from, it ain't C++? > It's microsoft specific extension to C++ language. It is basically a class that enforces only public pure virtual functions (disregarding if function is not declared virtual) , can inherit only from interface classes, and also does not have vtable nor does initialize vptr in order to reduce code size. Commonly used for microsoft com programming. Greetings, Branimir.
From: Daniel T. on 1 Jun 2007 09:41
obrianpatrick(a)gmail.com wrote: > How should the class YImpl be defined and implemented? Please note > that I can change the definition or implementation of XImpl but not > the definition of X and Y interfaces as they have come from some other > sources on which I have no control. Not really a question for comp.object, but the appropriate C++ solution is that the person who wrote class Y should have used virtual inheritance. Let him know of his bug. Stuart's idea is a nice work around though... Can this be turned into a comp.object question? [ X ] ^ | +---+---+ | | [ Y ] [XImpl] ^ ^ | | +---+---+ | [YImpl] The above could also be expressed as: [ X ] ^ | +-----+-----+ | | [ Y ] | ^ | | | [YImpl]<>---[XImpl] This can be done because there aren't any users of XImpl (all such users would go through the X interface. Translated into C++ code this would look like: class X { public: virtual void func1() = 0; }; class Y : virtual public X { public: virtual void func2() = 0; }; class XImpl : virtual public X { public: void func1() { } }; // the below could be "private XImpl, virtual public Y // but I'm trying to illustrate a point. class YImpl : public XImpl, virtual public Y { void func2() { } }; Or translated into Python: class XImpl: def func1(self): pass class YImpl (XImpl): def func2(self): pass People often comment on how association and containment look very much alike in code. Here we have an example where inheritance and containment look very much alike. |