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

How to do the implementation?

Given class A as the client of IFace and IFace is defined as:

int switcher;

class IFace
{
public:
virtual method1()=0;
virtual method2()=0;
};

class B : public IFace
{
// switcher is 0
C c;
// switcher is -1
(D d;)
// switcher is 1
(E e;)
};

c, d, or e is created at run-time but not more than one will.

How to do the implementation? Thanks!
Jul 22 '05 #1
3 1122
"foggy" <fo***@turboweb.com> wrote in message
news:N8********************@bgtnsc04-news.ops.worldnet.att.net...
Given class A as the client of IFace and IFace is defined as:

int switcher;

class IFace
{
public:
virtual method1()=0;
virtual method2()=0;
};

class B : public IFace
{
// switcher is 0
C c;
// switcher is -1
(D d;)
// switcher is 1
(E e;)
};

c, d, or e is created at run-time but not more than one will.

How to do the implementation? Thanks!

The question is quite abstract and imprecise. This said:

Instead of a single subclass of IFace, you should
probably have 3 subclasses that contain either C, D or E.
The right subclass will be instantiated/accessed based
on the value of switcher.
hth, Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Jul 22 '05 #2
"foggy" <fo***@turboweb.com> wrote in message news:<N8********************@bgtnsc04-news.ops.worldnet.att.net>...
Given class A as the client of IFace and IFace is defined as:

int switcher;

class IFace
{
public:
virtual method1()=0;
virtual method2()=0;
};

class B : public IFace
{
// switcher is 0
C c;
// switcher is -1
(D d;)
// switcher is 1
(E e;)
};

c, d, or e is created at run-time but not more than one will.

How to do the implementation? Thanks!


class B : public IFace
{
enum impl_variants { typeC = 0, typeD=-1, typeE=1 };
const impl_variants m_type;
union {
C* c;
D* d;
E* e;
} ptr;
public:
B::B( int switcher ) :
m_type( static_cast<impl_variants>( switcher ) )
{
switch( m_type )
{
case typeC: ptr.c = new C; break;
....

Regards,
Michiel Salters
Jul 22 '05 #3
> c, d, or e is created at run-time but not more than one will.
I take that to mean that switcher is not known at compile time? This is a
messy problem! We need more information to give a complete answer.

You'll have to use a union for C/D/E and store the initial value of switcher
in the class so it can be safely accessed later, unless switcher is constant
per run:
union
{
C c;
D d;
E e;
};

There is no way to conditionally embed an object except at compile time: if
C, D, or E has a constructor, you'll instead have to create it with new
(switch statement), store it in a pointer, and delete it in ~B() (which will
probably require another switch() ):
union
{
C *c;
D *d;
E *e;
};

If B does much more than hold IFace and one data member, and there is any
possibility of the data object being needed elsewhere, I'd consider using a
separate class (either an embedded member or a superclass) that holds C/D/E
and gives B orderly access to it.

It might also make sense, depending on what B is used for and how complex it
is, to make it a template by value (switcher) and provide specializations.
Then you could create a B<> object with new at runtime and it would have the
proper members.

"foggy" <fo***@turboweb.com> wrote in message
news:N8********************@bgtnsc04-news.ops.worldnet.att.net...
Given class A as the client of IFace and IFace is defined as:

int switcher;

class IFace
{
public:
virtual method1()=0;
virtual method2()=0;
};

class B : public IFace
{
// switcher is 0
C c;
// switcher is -1
(D d;)
// switcher is 1
(E e;)
};

c, d, or e is created at run-time but not more than one will.

How to do the implementation? Thanks!

Jul 22 '05 #4

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

Similar topics

3
by: jenniferyiu | last post by:
IMHO, simply NO. False actually, practically.
9
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
29
by: Enrico `Trippo' Porreca | last post by:
Both K&R book and Steve Summit's tutorial define a getline() function correctly testing the return value of getchar() against EOF. I know that getchar() returns EOF or the character value cast to...
52
by: lovecreatesbeauty | last post by:
Why the C standard committee doesn't provide a standard implementation including the C compiler and library when the language standard document is published? C works on the abstract model of low...
20
by: Luc Kumps | last post by:
(Sorry about the previous post, it got transmitted before it was complete) We try to separate implementation and interface defintions, but we run into a problem. I hope the guru's can solve this,...
7
by: desktop | last post by:
I the C++ standard page 472 it says that an associative container can be constructed like X(i,j,c) where i and j are input iterators to elements. But in the implementation there is no constructor...
6
by: Ralph | last post by:
Hi, I was reading effictive C++ and some other books again and they all tell you about hiding implementation details (proxy/pimpl/inheritance) but they never really explain when to use it. I...
0
by: anto.anish | last post by:
Hi , Since, i did not want to write instantiations in Source file of all template methods for various different datatypes that my client might use, i choose to write implementation of template...
1
by: anto.anish | last post by:
Hi , Since, i did not want to write explicit instantiations in Source file of all template methods for various different datatypes that my client might use, i choose to write implementation of...
173
by: Ron Ford | last post by:
I'm looking for a freeware c99 compiler for windows. I had intended to use MS's Visual C++ Express and use its C capability. In the past with my MS products, I've simply needed to make .c the...
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:
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: 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...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.