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 7604883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:01:30+00:00 2026-05-31T00:01:30+00:00

When extending a class, is there any difference in performance between polymorphism and composition?

  • 0

When extending a class, is there any difference in performance between polymorphism and composition? Take the following example using composition (in C++):

class Window
{
    public:
        Window(Renderer &renderer) : m_renderer(renderer)
        { }

        void update()
        {
            ....
            m_renderer.draw(this);
        }
    private:
        Renderer &m_renderer;
}

… and using polymorphism:

class Window : public Renderer
{
    public:
        virtual ~Window() {};

        void update()
        {
            ...
            draw();
        }
    protected:
        virtual void draw() = 0;
}

The composition version uses a reference as a member so I suppose that it requires a little more space, but is there any performance gain in either version?

Note: I have checked out similar post such as this, but is does not cover performance.

Thank you for your answers!

  • 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-31T00:01:32+00:00Added an answer on May 31, 2026 at 12:01 am

    Well, if you pose the question like this, then yes there is a memory/speed trade-off.

    1. Composition

    • 8 bytes (on 64 bits architecture) are likely to be used for the reference

    2. Inheritance

    • 8 bytes (on 64 bits architecture) if you need to make your class polymorphic (v-pointer)
    • some overhead for the attributes of the base class if any (note: inheriting from stateful classes is a code smell)
    • ~20% overhead for the virtual call (gross estimate, lots of variation there)

    So you might say that inheritance is slightly more costly… but really it’s just invisible. The overhead of a function call compared to the inner computation required for graphic rendering is just negligible.


    On the other hand, the semantics differ. Whether you inherit or not is significant. Is you Window a Renderer ? Does it make sense ?

    Write sane code first. Then optimize as (if !!!) needed.

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

Sidebar

Related Questions

Is there a function that tells what class you are extending? Like function_exists? I
I am extending a class defined in a library which I cannot change: public
I'm using my own class for database queries, extending mysqli: class iDatabase extends mysqli
I am extending a template class using C++ in Visual Studio 2005. It is
Given a class class RandomName extends CommonAppBase {} is there any way to automatically
Is there any way to listen to incoming calls by extending BroadcastReceiver to listen
I was wondering if there is a way for extending Font class in a
I'm using extending application class on Android to share my data across the entire
Consider the following simple code import java.util.*; public class MainTest<T extends Object1<?,?>> { List<T>
I have an intermediary class extending System.Web.UI.Page for all of my pages that require

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.