Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 304503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:17:12+00:00 2026-05-12T07:17:12+00:00

I am working in a class A that requires very high performance and am

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:17:12+00:00Added an answer on May 12, 2026 at 7:17 am

    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

    • Usually requires virtual destructors, this causes a slight overhead during deletion of the object, and increases the size by a ‘pointer’ (this 1 pointer is used by all virtual objects, so it makes it a lot cleaner)
    • Allows you to override a function so that it acts correctly for your object even when it is cast to the ‘base’ class. (Note: This does invoke a small overhead when calling the function)
    • The size of the class increases by the size of the ‘base’.
    • Any ‘base’ functions that aren’t virtual have NO overhead for calling them.
    • Generally a lot cleaner (so long as it makes sense to inherit)

    Pointing to “base” class

    • Requires you to dereference the pointer to the ‘base’ class everytime you want to call one of its functions (i.e. every ‘base’ function has overhead)
    • Impossible to override functions properly (so calling them on the ‘base’ class invokes the one in the container of the base) without using function pointers (which is possibly more overhead than virtual functions)
    • Requires separate allocations for both of the allocations, which usually have quite severe performance AND memory implications (allocating is slow, and usually allocations are aligned to a certain boundary, increasing their size, as well as storing extra information so the block can be properly deallocated).
    • Allows you to NOT allocate the base class, saving memory in that instance
    • Allows you to change what the ‘base’ class even after you have created it.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm working on this class that's supposed to request help documentation from a
I resolved this issue myself. It turns out that the activation framework requires some
I'm working on a class that inherits from another class, but I'm getting a
I'm working on a class that is storing a 2D array of class MyType
I'm currently working on a class that calculates the difference between two objects. I'm
I'm working on a sparse matrix class that needs to use an array of
I'm working on some code that uses the System.Diagnostics.Trace class and I'm wondering how
I am working in a project that has two main parts: a class library
I am currently working on a site that requires ACL and as I am
I have gotten everything working for my class in which I am using Tomcat

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.