Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two-Dimensional Array using Templates

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...

like image 850
fabian789 Avatar asked Dec 22 '11 17:12

fabian789


People also ask

What are two-dimensional array explain with example?

Here i and j are the size of the two dimensions, i.e I denote the number of rows while j denotes the number of columns. Example: int A[10][20]; Here we declare a two-dimensional array in C, named A which has 10 rows and 20 columns.

Can you create a 2 dimensional array with different types?

You can even create a two-dimensional array where each subarray has a different length or different type, also known as a heterogeneous array in Java.

What is multi dimensional array Give an example of a 2 dimensional array?

A multi-dimensional array is an array with more than one level or dimension. For example, a 2D array, or two-dimensional array, is an array of arrays, meaning it is a matrix of rows and columns (think of a table). A 3D array adds another dimension, turning it into an array of arrays of arrays.


2 Answers

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.

like image 53
Paolo Capriotti Avatar answered Oct 21 '22 04:10

Paolo Capriotti


Use default value for constructor to call it without argument i.e. Array(int index = 5);

Please check this: http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.3

Can one constructor of a class call another constructor of the same class to initialize the this object?

like image 35
Dmitriy Kachko Avatar answered Oct 21 '22 04:10

Dmitriy Kachko