c++ - What are the performance implications of inheriting a class vs including a ptr to an instance of the class as a member variable? -


I am working in a square "A" which requires a lot of performance and such implication I'm trying to work in a similar way. / P>

If I am the successor to this new class "B", then the memory profile of "A" should be increased by more than that.

If I include only one PTR in "B" as a member variable in "A", then I would be right to think that, until "B" is on the heap (i.e. new Dad up) then "A" will remain small because this is apart from the new PTR reference. Is there anything else I have not thought of?

This is a better architecture for the heirs of "B" for me, although this may be based on a better performance to work with the member as it works. I believe the heritage will be the best case here, but every situation is different, so here's the professionals and the opposition. Each choice

Inheritance

  • Usually requires virtual destructors, because of this there is a slight overhead during the extinction of the object, and ' Pointer '(this 1 pointer is used by all virtual objects, so it makes a lot cleaner)
  • Allows you to override the function so that it works correctly for your object , Even if it 'base' class (note: function When calling it, it invokes a small overhead)
  • Class increases by the size of 'base'.
  • Any 'base' function which
  • is usually very clean (unless it is understood for heir)

"base"

  • Every time you want to call an indicator in the 'base' class (ie the overhead of every 'base' function Is required).
  • It is impossible to override the work properly without using the function pointer (which is probably more overhead than the virtual function) (hence calling a 'base' class to one in the container of the base
  • Individually allocation for both the allocation, which is usually quite severe performance and memory effect (allocation is slow, and generally allocation is aligned with a certain extent, in their size Growth Only additional storage
  • allows you to not allocate the base class to save the memory on that basis
  • Allows you to change the 'base' class after this You have made it.

In fact, I think that in the form of optimization, it is probably one of the least important things to check, usually an algorithm (eg Adding to the initial migration) Nor will it create a celestial difference in comparison to such adaptation. The direction of the direction in which direction is the structure of the program, and on that note, I think the best advice I can give you It is:

Say, there are relationships in the class, if class A is class B, then you should inherit . If you say that class A is square b, then you should place it in class A from class A.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -