I am working in a class “A” that requires very high performance and am trying to work out the implications either way.
If I inherit this new class “B”, the memory profile of “A” should increase by that much.
If I include just a ptr to “B” as a member variable in “A”, then would I be correct in thinking that, as long as “B” is on the heap (ie new’d up) then “A” will remain as small as it is other than the new ptr reference. Is there something else that I haven’t thought of?
It is preferable architecture wise for me to inherit “B”, however it may be preferable performance wise to just stick with it as a member variable.
I believe inheritance would be the best case here, but every situation is different so here’s the pros and cons of each choice.
Inheriting
Pointing to “base” class
Really, I think that as an optimization, this is probably one of the least significant items to investigate. Usually a small change to an algorithm (such as adding in early escapes) will make an astronomical difference compared to this sort of optimization. What should guide this decision is the structure of the program, and on that note, I think the best advice I can give you is this:
Say, out-loud, the relationship the classes have, if its “Class A is a Class B” then you should inherit. If you say “Class A has a Class B” then you should keep a pointer within Class A to Class B.