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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:01:48+00:00 2026-05-26T02:01:48+00:00

Is it possible to find the size of a derived class object using a

  • 0

Is it possible to find the size of a derived class object using a base class pointer, when you don’t know the derived type.

Thank you.

  • 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-26T02:01:48+00:00Added an answer on May 26, 2026 at 2:01 am

    There’s no direct way, but you can write a virtual size() method child classes can implement. An intermediary templates class can automate the leg work.

    struct base {
      virtual size_t size() const =0;
      virtual ~base() { }
    };
    
    template<typename T> 
    struct intermediate : base {
      virtual size_t size() const { return sizeof(T); }
    };
    
    struct derived : intermediate<derived> 
    { };
    

    This does require your hierarchy be polymorphic… however, requesting behavior based on the dynamic type of an object rather than its static type is part of the definition of polymorphic behavior. So this won’t add a v-table to the average use case, since at the very least you probably already have a virtual destructor.

    This particular implementation does limit your inheritance tree to a single level without getting into multiple inheritance [ie, a type derived from derived will not get its own override of size]. There is a slightly more complex variant that gets around that.

    struct base { /*as before */ };
    
    template<typename Derived, typename Base>
    struct intermediate : Base {
      virtual size_t size() const { return sizeof(Derived); }
    };
    
    struct derived : intermediate<derived, base>
    { };
    
    struct further_derived : intermediate<further_derived, derived>
    { };
    

    Basically, this inserts an intermediate in between each actual layer of your hierarchy, each overriding size with the appropriate behavior, and deriving from the actual base type. Repeat ad nauseum.

    //what you want
    base >> derived 
         >> more_deriveder
         >> most_derivedest
    
    //what you get
    base >> intermediate<derived, base> 
         >> derived >> intermediate<more_deriveder, derived> 
         >> more_deriveder >> intermediate<most_derivedest, more_deriveder> 
         >> most_derivedest
    

    Several mixin-type libraries make use of such a scheme, such that the mixins can be added to an existing hierarchy without introducing multiple inheritance. Personally, I rarely use more than a single level of inheritance, so I don’t bother with the added complexity, but your mileage may vary.

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

Sidebar

Related Questions

is it possible to find out the page size of a processor using C
I have an pointer Base* base_ptr to an polymorphic object. Is it possible to
Possible Duplicate: Find the min number in all contiguous subarrays of size l of
Is it possible to find in DOM Inspector what tag/class/id combination triggered a particular
I'm writing a program to find the file size of files. Is it possible
Does anyone know if it is possible to find a power spectral density of
Anyone knows if it's possible to check APK size from IntelliJ? I cannot find
Possible Duplicate: How to find the sizeof( a pointer pointing to an array )
Possible Duplicate: How do I find the size of a struct? Struct varies in
today i ran into a possible size limitation of geography type Linestrings within Sql

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.