473,386 Members | 1,733 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.

Simple OO design issue

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

Jun 1 '07 #1
8 1850
ob***********@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.
Jun 1 '07 #2
ob***********@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.
Jun 1 '07 #3
Hi Stuart,
Thanks a ton. How neat!!
Patrick

On Jun 1, 2:39 pm, Stuart Redmann <DerTop...@web.dewrote:
obrianpatr...@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 -

Jun 1 '07 #4
On Jun 1, 12:08 pm, Ian Collins <ian-n...@hotmail.comwrote:
obrianpatr...@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.

Jun 1 '07 #5
ob***********@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.
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 work around is a nice idea though...
Jun 1 '07 #6
On Jun 1, 2:39 am, Stuart Redmann <DerTop...@web.dewrote:
obrianpatr...@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 -
Cool!!! Thats a very cool way of doing it. However, I have a feeling
that whenever you run in to such issues and have to resort to such
geeky way of doing things, it points to a design issue. Probably the
interface is not being used the way it was intended to be. Probably
we need a composition here rather than a inheritance (Class Z embeds
both class X and Y). Would really appreciate if someone out there
points out a scenario where such a construct is an absolute must.

Jun 1 '07 #7
Naresh Rautela <nr******@gmail.comwrote:
Cool!!! Thats a very cool way of doing it. However, I have a feeling
that whenever you run in to such issues and have to resort to such
geeky way of doing things, it points to a design issue. Probably the
interface is not being used the way it was intended to be. Probably
we need a composition here rather than a inheritance (Class Z embeds
both class X and Y). Would really appreciate if someone out there
points out a scenario where such a construct is an absolute must.
Well yes, YImpl could contain an XImpl object and delegate to it, but
that would be effectively the same as Stuart's idea and take more typing.
Jun 1 '07 #8
On Jun 1, 4:07 pm, Naresh Rautela <nraut...@gmail.comwrote:
On Jun 1, 2:39 am, Stuart Redmann <DerTop...@web.dewrote:
obrianpatr...@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
How should the class YImpl be defined and implemented? Please note
......
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
}
};


Cool!!! Thats a very cool way of doing it. However, I have a feeling
that whenever you run in to such issues and have to resort to such
geeky way of doing things, it points to a design issue. Probably the
interface is not being used the way it was intended to be. Probably
we need a composition here rather than a inheritance (Class Z embeds
both class X and Y). Would really appreciate if someone out there
points out a scenario where such a construct is an absolute must.
Well this is standard com issue, except that he should specify
__stdcall calling convention to produce valid COM objects.
For example, this is also perfectly legal way of doing it:
__interface X
{
void __stdcall func1(int);
};

__interface Y: X
{
void __stdcall func2(int);
};

struct XImpl
{
void* vptr;
int j;
static void __stdcall func1(XImpl*x,int i)
{
cout << "In XImpl::func 1: i = " << i;
x->j = i;
}
};

struct YImpl
{
XImpl x;
YImpl();
static void __stdcall func2(YImpl*y,int i)
{
cout << "In YImpl::func 2: i = " << i << " j = " << y->x.j;
}
};

typedef void (__stdcall *func_t)(YImpl*,int);

struct YImplVtable{
func_t func1,func2;
}vty = { (func_t)&XImpl::func1,&YImpl::func2 };

YImpl::YImpl(){x.vptr=&vty;}

and in program:

YImpl ii;
Y* y = (Y*)&ii;
y->func1(42);
y->func2(1);

Actually laying down objects and vtables is only way to
program COM objects in C. If C++ provides layout
compatibility it has convenient syntax.
Interfaces just provide vtable layout, can't be used for
casual C++ programming (no virtual destructors so user must
provide function for deletition, only public
non virtual inheritance allowed ...)

Greetings, Branimir.

Jun 1 '07 #9

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

Similar topics

20
by: Sam | last post by:
Hi I'm learning to code with C++ and wrote some very simple code. I think it's consistent with every rule but always got compiling errors that I don't understand. The code include 5 files as...
19
by: Dales | last post by:
I have a custom control that builds what we refer to as "Formlets" around some content in a page. These are basically content "wrapper" sections that are tables that have a colored header and...
27
by: one man army | last post by:
Hi All- I am new to PHP. I found FAQTS and the php manual. I am trying this sequence, but getting 'no zip string found:'... PHP Version 4.4.0 $doc = new DomDocument; $res =...
18
by: Bob Cummings | last post by:
Not sure if this is the correct place or not. Anyhow in school we were taught that when trying to calculate the efficiency of an algorithm to focus on something called FLOPs or Floating Point...
4
by: Shawnk | last post by:
This post is intended to verify that true value semantics DO NOT EXIST for the Enum class (relative to boolean operations). If this is true then (thus and therefore) you can not design state...
4
by: saintor1 | last post by:
Access 97 - I want the date format YYYY-M M-DD to show everywhere. My problem is that I deal with French and English versions. And don't want to play with Windows Regional Settings. In the...
5
by: pgrazaitis | last post by:
I cant seem to get my head wrapped around this issue, I have myself so twisted now there maybe no issue! Ok so I designed a class X that has a few members, and for arguments sake one of the...
4
by: Ken Fine | last post by:
I've been living with a frustrating issue with VS.NET for some months now and I need to figure out what the problem is. Hopefully someone has run into the same issue and can suggest a fix. I...
7
by: CSharper | last post by:
Yesterday I had a heated discussion with my colleagues on what is a data centric application and having business logic in sql. I have group of people who wants to include all the business logic in...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.