M
ma740988
Consider a class - lets call it CObjectHolderClass that has a
collection of object pointers like
std::vector<CBaseClass*> base_class_vec;
as member var. Now somewhere in my CObjectHolderClass I do
CDerived1* p_derived1 = new CDerived1();
base_class_vec.push_back(derived1);
In CObjectHolderClass, i just use the public interface of CBaseClass.
If I derive another class - lets say CDerived2 from CBaseClass, I have
to change my CObjectHolderClass to do something like
CDerived2* p_derived2 = new CMyOtherBaseClass();
base_class_vec.push_back(p_derived2);
Trouble is, I have to modify CObjectHolderClass for different derived
classes. How do I get around this?
My guess is this borders on some type of 'design pattern' (an area I
haven't delved into yet) or perhaps a template solution but I'm
confused on how.
Untested or working source examples would be greatly appreaciated.
This alleaviates the need for me my potentially silly follow up
questions.
As always, thanks in advance.
collection of object pointers like
std::vector<CBaseClass*> base_class_vec;
as member var. Now somewhere in my CObjectHolderClass I do
CDerived1* p_derived1 = new CDerived1();
base_class_vec.push_back(derived1);
In CObjectHolderClass, i just use the public interface of CBaseClass.
If I derive another class - lets say CDerived2 from CBaseClass, I have
to change my CObjectHolderClass to do something like
CDerived2* p_derived2 = new CMyOtherBaseClass();
base_class_vec.push_back(p_derived2);
Trouble is, I have to modify CObjectHolderClass for different derived
classes. How do I get around this?
My guess is this borders on some type of 'design pattern' (an area I
haven't delved into yet) or perhaps a template solution but I'm
confused on how.
Untested or working source examples would be greatly appreaciated.
This alleaviates the need for me my potentially silly follow up
questions.
As always, thanks in advance.