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

  • Home
  • SEARCH
  • 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 7061991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:32:27+00:00 2026-05-28T04:32:27+00:00

Problem: I need an array (datamember) in a parent class A. However, depending on

  • 0

Problem: I need an array (datamember) in a parent class A.
However, depending on the child class, that array should have a different number of elements.

Is this possible?
If yes, how can I do this? (without dynamic allocation)
If no, then is there any way around? (without dynamic allocation)

Thanks

  • 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-28T04:32:28+00:00Added an answer on May 28, 2026 at 4:32 am

    It depends on how you plan on using said parent class, but you can accomplish what you’ve described using templates.

    template<size_t N>
    class Parent
    {
        int data[N];
    }
    
    class Child : public Parent<4>
    {
    }
    

    This has many limitations (such as being unable to use a Parent class pointer really), but it avoids having the dynamic allocation.

    EDIT — Since you need true access to a parent array, here is an alternative.

    class Parent
    {
        const size_t mSize;
    
    public:
        Parent(size_t size) : mSize(size) {}
    
        inline size_t Size() { return mSize; }
        virtual int* GetData() = 0;
    }
    
    template<size_t N>
    class Child : public Parent
    {
        int mData[N];
    
    public:
        Child() : Parent(N) {}
    
        virtual int* GetData() { return mData; }
    }
    
    class Grandchild : public Child<8>
    {
    public:
        GrandChild() {}
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with some numpy stuff. I need a numpy array to
The Problem: I need to receive different types of content from a number of
I have the following problem. I have an array of bytes that I want
I have a PHP array that I need to sort. I have included the
The Problem I need some properties of each item that query returns as array
Hello i have a problem with c# Arrays. I need a array to store
I have a problem where I need an array of arrayList. For example if
I have the following problem i need in array 3 the languages names not
I need to convert array of Objects into a Long/Integer.. Problem is that those
I have a problem that I need some help on but I feel I'm

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.