Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can a base class be its own factory?

Reply
Thread Tools

Can a base class be its own factory?

 
 
shaun roe
Guest
Posts: n/a
 
      03-09-2006
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
 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      03-09-2006

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.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-09-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 14 04-03-2010 10:08 AM
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 0 04-01-2010 10:25 PM
Its a bird, its a plane, no ummm, its a Ruide thunk Ruby 1 03-30-2010 11:10 AM
I have built my own (simple) thread manager [TM], but just found java 5 has its own. Saverio M. Java 0 07-03-2006 08:52 AM
Access of base class' private base class: qualification required, why Alf P. Steinbach C++ 6 09-03-2005 04:03 PM



Advertisments