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

Can a base class be its own factory?

If I have a baseclass A and have subclasses B and C, can I make a
parametrized constructor for A, say A(chooseClass), which then uses the
parameter to do:
A * p_theBaseclass;
if (chooseClass == 0){
p_theBaseclass = new B;
} else {
p_theBaseclass = new C;
}

and then for all the methods which A describes, delegate the
implementation to B or C? so for example if they all have method
'whoAmI', I could do:

A * p_myClass = new A(0);
cout<< p_myClass->whoAmI()<<endl; //returns 'I am B'

A * p_myClass = new A(1);
cout<< p_myClass->whoAmI()<<endl; //returns 'I am C'
or Does It Get A Little Confused?

this is all off the top of my head so probably full of craziness and
mistakes.
cheers
s
Mar 9 '06 #1
2 1811

shaun roe wrote:
If I have a baseclass A and have subclasses B and C, can I make a
parametrized constructor for A, say A(chooseClass), which then uses the
parameter to do:
A * p_theBaseclass;
if (chooseClass == 0){
p_theBaseclass = new B;
} else {
p_theBaseclass = new C;
}

and then for all the methods which A describes, delegate the
implementation to B or C? so for example if they all have method
'whoAmI', I could do:

A * p_myClass = new A(0);
cout<< p_myClass->whoAmI()<<endl; //returns 'I am B'

A * p_myClass = new A(1);
cout<< p_myClass->whoAmI()<<endl; //returns 'I am C'
or Does It Get A Little Confused?

this is all off the top of my head so probably full of craziness and
mistakes.


No. It would be a really bad idea to try and replace 'this' inside a
constructor. Create a factory method and make it static.

class A
{
static A * factory(params) { A * ret = 0; if (xxx) ret = new B();
else ret = new C(); return ret; }
};

A * a = A::factory(params);

You will notice a lot of dependencies here though. The base must know
about all of its subclasses this way. Might want to look into an
Abstract Factory instead.

Mar 9 '06 #2
shaun roe wrote:
If I have a baseclass A and have subclasses B and C, can I make a
parametrized constructor for A, say A(chooseClass), which then uses the
parameter to do:
A * p_theBaseclass;
if (chooseClass == 0){
p_theBaseclass = new B;
} else {
p_theBaseclass = new C;
}
You can (or, rather, nothing is there to stop you), except that it's
a BAD IDEA(tm).
and then for all the methods which A describes, delegate the
implementation to B or C? so for example if they all have method
'whoAmI', I could do:

A * p_myClass = new A(0);
cout<< p_myClass->whoAmI()<<endl; //returns 'I am B'

A * p_myClass = new A(1);
cout<< p_myClass->whoAmI()<<endl; //returns 'I am C'
or Does It Get A Little Confused?

this is all off the top of my head so probably full of craziness and
mistakes.


If you have to make those three classes inter-related, perhaps it's better
to make 'B' and 'C' _nested_ in 'A'...

V
--
Please remove capital As from my address when replying by mail
Mar 9 '06 #3

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

Similar topics

14
by: Sridhar R | last post by:
Consider the code below, class Base(object): pass class Derived(object): def __new__(cls, *args, **kwds): # some_factory returns an instance of Base # and I have to derive from this...
11
by: Riccardo Galli | last post by:
Hi all. It's easier if I show an example first. Say I have class A(object): def speak(self): print 'A!' class B(object): def speak(self): print 'B!'
5
by: Martin Magnusson | last post by:
Hi! I have a class with a private member which is a pointer to an abstract class, which looks something like this: class Agent { public: void Step( Base* newB ); private:
3
by: Teis Draiby | last post by:
I want to write a base class that includes a member function that creates an instance of a derrived class and returns a pointer to it. Problem: The derived class definition has to follow the base...
6
by: Busin | last post by:
Classes B, C, D and E are derived from base class A. A* CreateB(); A* CreateC(); A* CreateD(); A* CreateE(); class X { public:
1
by: Mark McDonald | last post by:
This question kind of follows on from Mike Spass’ posting 10/11/2004; I don’t understand why you can’t declare an implicit operator to convert a base class to a derived class. The text...
4
by: Rachel Devons | last post by:
All, I'm struggling with an OOP concept. Let me try to define what I'm wanting by using some classic examples. Let's say that I have classes called Square & Circle that derive from class...
2
by: reckless2k | last post by:
Client side; knows nothing of Derived: class Base { ... virtual void do_something() } #include "Base.h" void main() {
5
by: alexl | last post by:
I have 2 classes, class base { public: some virtual functions } class derived : public base { public:
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.