Can a base class be its own factory?

S

shaun roe

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
 
N

Noah Roberts

shaun said:
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.
 
V

Victor Bazarov

shaun said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top