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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:48:43+00:00 2026-06-17T10:48:43+00:00

I have a class that is designed to be general-purpose, used wherever, that looks

  • 0

I have a class that is designed to be general-purpose, used wherever, that looks kinda like this:

class FixedByteStream {
public:
  FixedByteStream(const char* source)
  {
    size = strlen(source);
    copy(source);
  }

  /* Many more constructors here */

protected:
  void copy(const char* source)
  {
    address = allocate();
    //...
  }

  /* Plus other functions that call allocate() */

  char* FixedByteStream::allocate()
  {
    return (char*)malloc(size);
  }
}

I have then extended this class, so that it can use a project-specific memory pool.

class PooledByteStream : public FixedByteStream {
public:
  PooledByteStream::PooledByteStream() : FixedByteStream() {}

protected:
  char* PooledByteStream::allocate()
  {
    return (char*)PooledByteStream::pool.allocate(size);
  }
}

PooledByteStream is supposed to be identical to a FixedByteStream, with all the same functions and constructors, except that when allocate() is called, it should be retrieving pointers from a memory pool.

However, PooledByteStream::allocate() is not ever called. Not from the inherited constructors, nor from other inherited functions (that call the inherited copy()). Anything inherited from the base class is completely oblivious to the fact that allocate() ought to do something quite different now.

The question is, how do I fix that? How do I make inherited functions call overridden functions, rather than those of the base class? Copy-pasting all the necessary functions from the base class would obliterate the point of inheritance, so I’m assuming that’s not the answer here.

NOTE: I am not looking for advice on memory management, or other ways to reach the same end result. This is just an example!

  • 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-06-17T10:48:44+00:00Added an answer on June 17, 2026 at 10:48 am

    You need to declare allocate() as virtual in order to override it. However, a base class constructor cannot call a derived class’s overrides because the derived class has not been constructed yet, and a base class destructor cannot call a derived class’s overrides because the derived class has already be destructed.

    If you must call allocate() in the base class constructor, you can use a template to get around the restriction, eg:

    template<typename Derived>
    class FixedByteStreamBase
    {
    public:
      FixedByteStreamBase(const char* source)
      {
        size = strlen(source);
        copy(source);
      }
    
      /* Many more constructors here */
    
    protected:
      void copy(const char* source)
      {
        address = Derived::allocate();
        //...
      }
    
      /* Plus other functions that call allocate() */
    };
    
    class FixedByteStream : public FixedByteStreamBase<FixedByteStream>
    {
    public:
        static char* allocate()
        {
            return (char*)malloc(size);
        }
    };
    
    class PooledByteStream : public FixedByteStreamBase<PooledByteStream>
    {
    public:
        static char* allocate()
        {
            return (char*)pool.malloc(size);
        }
    };
    

    Or:

    struct MallocAllocator
    {
        static char* allocate()
        {
            return (char*)malloc(size);
        }
    };
    
    struct PoolAllocator
    {
        static char* allocate()
        {
            return (char*)pool.allocate(size);
        }
    };
    
    template<typename Allocator>
    class FixedByteStreamBase {
    public:
      FixedByteStreamBase(const char* source)
      {
        size = strlen(source);
        copy(source);
      }
    
      /* Many more constructors here */
    
    protected:
      void copy(const char* source)
      {
        address = Allocator::allocate();
        //...
      }
    
      /* Plus other functions that call allocate() */
    };
    
    typedef FixedByteStreamBase<MallocAllocator> FixedByteStream;
    typedef FixedByteStreamBase<PoolAllocator> PooledByteStream;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class that looks like this: class A { public: class variables_map vm
I have a base Color class that looks something like this. The class is
I have an Extension class for handling general functions that's designed to be used
Assume I have a class like this: public class Foo { public Bar RequiredProperty
I have some C# class libraries, that were designed without taking into account things
I have a Class that has a private variable with a public setter/getter function:
I have a class (class A) that is designed to be inherited by other
I have a variation on a Quantity (Fowler) class that is designed to facilitate
The basic_string class was apparently designed as a general purpose container, as I cannot
I have a class that is designed primarily as a POCO class, with various

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.