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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:20:08+00:00 2026-05-27T15:20:08+00:00

I am trying to implement a dynamic array: template <typename Item> class Array {

  • 0

I am trying to implement a dynamic array:

template <typename Item>
class Array {
private:
    Item *_array;
    int _size;
public:
    Array();
    Array(int size);
    Item& operator[](int index);
};

template <typename Item>
Array<Item>::Array() {
    Array(5);
}

template <typename Item>
Array<Item>::Array(int size) {
    _size = size;
    _array = new Item [size];

    for (int i = 0; i < size; i++)
        cout << i << " " << _array[i] << " " << &_array[i] << endl;
}

template <class Item>
Item& Array<Item>::operator[](int index) {
    if (index < 0 || index > _size-1)
        cout << "this: " << this << ". Index out of range" << endl;

    return _array[index];
}

When used like this, it works as expected, i.e. prints 5:

Array< int > testArray(5);
testArray[0] = 5;
cout << testArray[0] << endl;

However, I would like to use the class for a two-dimensional dynamic array. I thought that the following would just magically work and print 5…

Array< Array<int> > testArray(5);
testArray[0][0] = 5;
cout << testArray[0][0] << endl;

…but it does not work. It crashes when I try to set the value at [0][0]. The debugger shows me that this has _size set to 0 and _array to NULL. this at that point points to the first element of the _array of the last created Array instance.

One of the things I don’t get is when the “inner” array calls its constructor. Stepping through the code, I see that Array(int size) is called once and Array() five times. I would like to create the inner array with a specific size, but using Array< Array<int>(10) > testArray(5) does not compile.

Could you provide me with some insight on this? It seems I could not quite wrap my head around templates yet…

  • 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-27T15:20:09+00:00Added an answer on May 27, 2026 at 3:20 pm

    You cannot chain constructor calls in C++. Your first constructor implementation does nothing, so the 5 instances contained in the parent Array end up being uninitialized, resulting in undefined behavior.

    To fix, you can either add a default value to the size parameter of the other constructor, or factor out the initialization logic in a separate (private) function, and call it from both constructors.

    EDIT: The reason why the first constructor does nothing is that the line

    Array(5)
    

    does not call the constructor of the current instance, but instead allocates a new (unnamed) temporary Array instance, which is immediately destructed at the end of the line.

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

Sidebar

Related Questions

I'm trying to implement dynamic array in C using realloc(). My understanding is that
I'm trying to implement a dynamic OPTION list in JavaScript. Depending on other selections
I am trying to implement a dynamic search jquery and php, but I am
I'm trying to implement a function that generates dynamic queries for MNesia. For example,
I am trying to implement a service with a public api which access control
I'm trying to implement dynamic reloading objects in Python, that reflect code changes live.
I've been trying to implement a dynamic multi-model form with accepts_nested_attributes_for in my rails
I'm trying to implement a Paypal button with a dynamic value. When I enter,
I am trying to implement a generic Wrapper-Class for Qt's class system using C#'s
Any help would be appreciated on this one. I'm trying to implement a dynamic

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.