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
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