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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:24:41+00:00 2026-06-17T14:24:41+00:00

I have an array as a member of a class. In a subclass, I

  • 0

I have an array as a member of a class. In a subclass, I would like to re-define the array with a different size. I want to do this because I anticipate making many subclasses, each with only the array size it needs, and nothing more.

class Foo
{
    Foo() {ivar = 1};
    int thisArray[2];
    int ivar;
}

class Bar : public Foo
{
    Bar() {ivar = 3};
    int thisArray[4];
}

int main()
{
    Foo myFoo;
    Bar myBar;

    Foo fooCollection[] = {myFoo,myBar};

    cout << "myFoo array size = " << sizeof(myFoo.thisArray)/sizeof(int) << endl;
    cout << "myBar array size = " << sizeof(myBar.thisArray)/sizeof(int) << endl;

    for (int n=0;n<2;n++)
    {
        cout << "fooCollection[" << n << "] array size = ";
        cout << sizeof(fooCollection[n].thisArray)/sizeof(int) << endl;
    }
    for (int n=0;n<2;n++)
    {
        cout << "fooCollection[" << n << "] ivar = ";
        cout << fooCollection[n].ivar << endl;
    }

}

My results are:

myFoo array size = 2
myBar array size = 4
fooCollection[0] array size = 2
fooCollection[1] array size = 2
fooCollection[0] ivar = 1
fooCollection[1] ivar = 3

I get that, since I declare the array objects as objects of class Foo, that referring to myBar within that scope would reference myBar as though it was a Foo and consequently interpret the size of thisArray as equivalent to 2. I also understand why ivar comes out the way it does.

Is there a way to affect the size of thisArray within the Bar class so its “correct” size can be recognized within an array of Foo objects? I would use a vector, but they are not friendly on the arduino platform. I could also simply make the array within the Foo class with a size of 100, but I am trying to be conscious of memory allocation.

  • 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-17T14:24:42+00:00Added an answer on June 17, 2026 at 2:24 pm

    When you do this: cout << sizeof(fooCollection[n].thisArray)/sizeof(int) << endl;, it is impossible to know the size of thisArray because you are not using actual polymorphism. So the compiler assumes all elements in fooCollection are simpy Foo (static binding).

    Start by using pointers:

    Foo * fooCollection[] = { &myFoo, &myBar };
    

    And declaring a virtual member that will know at runtime the size of the array. (dynamic binding)

    virtual int size() {return sizeof(thisArray);}
    

    And then rewriting to:

    cout << fooCollection[n]->size()/sizeof(int) << endl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have a static char array member initialized in terms of
I want to have an array as a private class member. I use Itcl
Hello I have this question: I would like to have a vector as class
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
Let's say you have a c++0x std::array member of a template class and you
If I have a class with an array as a member: class A {
I have a Member class that has an array of MemberRoles attached to it.
I have a class with Array as a class member. And I have many
I have a class where one of the member variables is an array. I
I have a static const array class member (const pointers to SDL_Surfaces, but that's

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.