lundi 29 août 2016

Inheritance and member functions

Consider I have series of derived classes for example as listed below:

class A
{
...
}

class B1 : public A //there may be many B's here say, B2, B3 etc
{
 ...
}
class C1 : public B1 //may be more C's as well
{
...
}

I would like to put all of the objects in single container, thus all would be of type class A.

Suppose I would like to add a function to class C1, what would be the best way to achieve this? My options would be introducing it in the base class A and write the needed implementation in C1, or, I could introduce it in C1 and do dynamic casting to access it. Which one is preferred? Is dynamic casting too expensive? (My main constrain is the run time.I have a flag in the base class to indicate what type of derived object it is, thus I do not have to dynamic cast every object in the container. Does adding unnecessary functions to base class can result in bad instruction cache use?)

Aucun commentaire:

Enregistrer un commentaire