This is a pretty basic question but I’m still unsure:
If I have a class that will be instantiated millions of times — is it advisable not to derive it from some other class? In other words, does inheritance carry some cost (in terms of memory or runtime to construct or destroy an object) that I should be worrying about in practice?
Example:
class Foo : public FooBase { // should I avoid deriving from FooBase?
// ...
};
int main() {
// constructs millions of Foo objects...
}
Inheriting from a class costs nothing at runtime.
The class instances will of course take up more memory if you have variables in the base class, but no more than if they were in the derived class directly and you didn’t inherit from anything.
This does not take into account
virtualmethods, which do incur a small runtime cost.tl;dr: You shouldn’t be worrying about it.