Hello,
Currently I have the following "design":
class A { ... };
class B : public A { ... };
class C : public A { ... };
class Z {
private:
A *a;
public:
Z() { a = new A(); };
~Z() { delete a; };
A *GetObject() { return a; };
};
int main() {
Z *z = new Z();
A *temp = z->GetObject();
delete z;
return 0;
}
Now I want Z being able to use B or C in stead of A. So when creating Z
I should somehow be able to specify what it should use (A, B or C).
The easiest way that comes to mind is using templates to specify what
class type I want to use in my instance of Z.
template<class T> Z {
private:
T *t;
public:
Z() { t = new T(); };
~Z() { delete t; };
T *GetObject() { return t; }
};
int main() {
Z<C> *z = new Z<C>();
C *temp = z->GetObject();
delete z;
return 0;
}
But I would like to avoid templates (plans for porting the code to
embedded). Is there a better/other way of doing this?
TIA
Yves 4 3054
Yves Dhondt wrote: Now I want Z being able to use B or C in stead of A. So when creating Z I should somehow be able to specify what it should use (A, B or C). The easiest way that comes to mind is using templates to specify what class type I want to use in my instance of Z.
You could try this:
class Z {
private:
A *a;
public:
enum contained_type {
ctA,
ctB,
ctC
};
Z(contained_type t) {
switch (t) {
case ctA:
a = new A();
break;
case ctB:
a = new B();
break;
case ctC:
a = new C();
break;
}
};
~Z() { delete a; };
A *GetObject() { return a; };
};
There are other options too, of course. You could do something like this:
class Z {
private:
A *a;
public:
Z(A* p) : p(a) {};
~Z() { delete a; };
A *GetObject() { return a; };
};
Indi
--
Mark A. Gibbs (aka. Indi)
Administrator
#c++ on irc.Rizon.net http://ca.geocities.com/in***@rogers.com/
(temporary website)
Mark A. Gibbs wrote: Yves Dhondt wrote:
Now I want Z being able to use B or C in stead of A. So when
creating Z I should somehow be able to specify what it should use (A, B or
C). The easiest way that comes to mind is using templates to specify what
class type I want to use in my instance of Z. You could try this:
class Z { private: A *a; public: enum contained_type { ctA, ctB, ctC };
Z(contained_type t) { switch (t) { case ctA: a = new A(); break; case ctB: a = new B(); break; case ctC: a = new C(); break; } }; ~Z() { delete a; }; A *GetObject() { return a; }; };
There are other options too, of course. You could do something like
this: class Z { private: A *a; public: Z(A* p) : p(a) {}; ~Z() { delete a; }; A *GetObject() { return a; }; };
Indi
-- Mark A. Gibbs (aka. Indi) Administrator #c++ on irc.Rizon.net
http://ca.geocities.com/in***@rogers.com/ (temporary website)
Is the question about how to implement the factory????
demn, but it looks like it
Yves Dhondt wrote: Hello,
Currently I have the following "design":
class A { ... };
Make sure A has a virtual destuctor (in this case).
class A { public: virtual ~A(){} };
class B : public A { ... }; class C : public A { ... };
class Z { private: A *a; public: Z() { a = new A(); };
don't need ';' after func defs.
~Z() { delete a; };
don't need ';' after func defs.
A *GetObject() { return a; };
don't need ';' after func defs.
};
int main() { Z *z = new Z();
A *temp = z->GetObject();
delete z; return 0; }
Now I want Z being able to use B or C in stead of A. So when creating Z I should somehow be able to specify what it should use (A, B or C). The easiest way that comes to mind is using templates to specify what class type I want to use in my instance of Z.
template<class T> Z { private: T *t; public: Z() { t = new T(); };
don't need ';' after func defs.
~Z() { delete t; };
don't need ';' after func defs.
T *GetObject() { return t; } };
class Z {
private:
A *a;
Z() {}
public:
Z( const char * key )
: a( FactoryForA( key ) )
{}
~Z() { delete a; }
A *GetObject() { return a; };
};
There is a generic factory example in the Austria C++ library.
In this case you would "register" the factories for B and C (and A if A
can be instantiated).
AT_MakeFactory0P( "B", B, A, DKy );
AT_MakeFactory0P( "C", C, A, DKy );
These registrations would be in a .cpp file and not visible to the rest
of the program.
A * FactoryForA( const char * key )
{
A * a = at::FactoryRegister< Interface, DKy>::Get().Create( key )();
// might return null a - probably need to throw if that's the case
return a;
}
Z zb( "B" ); // Will create a B object
Z zc( "C" ); // Will create a C object
What's the purpose of the Z wrapper ? It looks awfully like a std::auto_ptr.
Disclaimer : all the code I wrote above is intested ... int main() { Z<C> *z = new Z<C>();
C *temp = z->GetObject();
delete z; return 0; }
But I would like to avoid templates (plans for porting the code to embedded). Is there a better/other way of doing this?
TIA
Yves
"Yves Dhondt" <no@privacy.net> wrote in message
news:41***********************@news.skynet.be...
| Hello,
|
| Currently I have the following "design":
|
| class A { ... };
| class B : public A { ... };
| class C : public A { ... };
|
| class Z {
| private:
| A *a;
| public:
| Z() { a = new A(); };
| ~Z() { delete a; };
| A *GetObject() { return a; };
| };
|
| int main() {
| Z *z = new Z();
|
| A *temp = z->GetObject();
|
| delete z;
| return 0;
| }
|
| Now I want Z being able to use B or C in stead of A. So when creating Z
| I should somehow be able to specify what it should use (A, B or C).
| The easiest way that comes to mind is using templates to specify what
| class type I want to use in my instance of Z.
|
| template<class T> Z {
| private:
| T *t;
| public:
| Z() { t = new T(); };
| ~Z() { delete t; };
| T *GetObject() { return t; }
| };
|
| int main() {
| Z<C> *z = new Z<C>();
|
| C *temp = z->GetObject();
|
| delete z;
| return 0;
| }
|
| But I would like to avoid templates (plans for porting the code to
| embedded). Is there a better/other way of doing this?
Polymorphism might be what you're looking for ?
Here's a quick example, if you don't mind the syntax:
class A { public: virtual void P()
{ std::cout << "A::P()\n"; } };
class B : public A { public: void P()
{ std::cout << "B::P()\n"; } };
class C : public A { public: void P()
{ std::cout << "C::P()\n"; } };
class Z
{
private:
A *a;
public:
Z( A* aa ) : a( aa ) {}
~Z() { delete a; };
A *GetObject() { return a; };
};
int main() {
Z *z = new Z( new B );
A *temp = z->GetObject();
temp->P();
delete z;
z = new Z( new C );
temp = z->GetObject();
temp->P();
delete z;
return 0;
}
Cheers.
Chris Val
PS: I have gone along with you're naming scheme, but
please choose better names for your identifiers
if you can. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Eloff |
last post by:
This is not really Python specific, but I know Python programmers are
among the best in the world. I have a fair understanding of the
concepts involved, enough to realize that I would benefit from...
|
by: Andre |
last post by:
Hi,
I've built a math library that does some matrix multiplications and
other linear algebric functions. I read some place about generics or
class templates and I was wondering if this will help...
|
by: Bo Xu |
last post by:
Object of Combination
By Bo Xu
Introduction
A combination of n things, taken s at a time, often referred as an
s-combination out of n, is a way to select a subset of size s from a
given set of...
|
by: PengYu.UT |
last post by:
Hi,
I heard that debug some C++ templates is very difficult. I'm wondering
whether it is possible to compile C++ program with templates to pure C
or C++ program without templates?
Best...
|
by: Audrey Pratt |
last post by:
Happy Holidays to you and allow us to play Santa this year with these awsome deals that in anyway you can refuse:
2000 Web Templates for only $18.00 (Savings Over $1,000.00)
...
|
by: Ted |
last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because
they were rejected in comp.lang.c++.moderated. It behooves anyone reading
them to first read the the thread of the...
|
by: liljencrantz |
last post by:
Hi,
I have a piece of code that uses hashtables to store pointers to
various bits of data. The hashtable sees all pointers as const void *,
while the application obviously uses various other...
|
by: Cephalobus_alienus |
last post by:
Hello,
I know that macros are evil, but I recently came across a problem
that I couldn't figure out how to solve with templates.
I wanted to create a set of singleton event objects, and wrote...
|
by: madhu.srikkanth |
last post by:
Hi,
I came across a paper by Angelika Langer in C++ Users Journal on
Expression Templates.
In the article she had mentioned that the code snippet below used to
calculate a dot product is an...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| | |