Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multidimensional array: operator overloading

I have a class with a multidimensional array:

  • it is possible to create a one, two, ..., n dimensional array with this class

  • if the array has n dimensions, i want to use n operator[] to get an object:

example:

A a({2,2,2,2}]; 
a[0][1][1][0] = 5;

but array is not a vector of pointer which lead to other vectors etc...

so i want the operator[] to return a class object until the last dimension, then return a integer

This is a strongly simplified code, but it shows my problem:

The error i receive: "[Error] cannot convert 'A::B' to 'int' in initialization"

#include <cstddef>     // nullptr_t, ptrdiff_t, size_t
#include <iostream>    // cin, cout...

class A {
    private:
        static int* a;
    public:
        static int dimensions;
        A(int i=0) { 
            dimensions = i;
            a = new int[5];
            for(int j=0; j<5; j++) a[j]=j; 
        };

        class B{
            public:
                B operator[](std::ptrdiff_t);
        };
        class C: public B{
            public:
                int& operator[](std::ptrdiff_t);
        };

        B operator[](std::ptrdiff_t);
};

//int A::count = 0;

A::B A::operator[] (std::ptrdiff_t i) {
    B res;
  if (dimensions <= 1){
    res = C();
}
  else{
    res = B();
  }
  dimensions--;
  return res;
}

A::B A::B::operator[] (std::ptrdiff_t i){
    B res;
    if (dimensions <=1){
        res = B();
    }
    else{
        res = C();
    }
    dimensions--;
    return res;
}

int& A::C::operator[](std::ptrdiff_t i){
    return *(a+i);
}


int main(){
    A* obj = new A(5);
    int res = obj[1][1][1][1][1];
    std::cout<< res << std::endl;
}
like image 221
Marius Küpper Avatar asked Oct 30 '14 23:10

Marius Küpper


People also ask

How to overload operators in a matrix?

Recommended: Please try your approach on {IDE} first, before moving on to the solution. To overload +, –, * operators, we will create a class named matrix and then make a public function to overload the operators. Let there are two matrix M1 [] [] and M2 [] [] of same dimensions.

How many times operator [] is called for a 3-dimensional array?

In this example for the 3 dimensional array, operator [] is only called only once for the first dimension which is 0. template <class T, unsigned ...

How to overload + – * and * operators in Python?

To overload +, –, * operators, we will create a class named matrix and then make a public function to overload the operators. Let there are two matrix M1 [] [] and M2 [] [] of same dimensions.

How to use operators in succession in a multi-dimensional array?

If you want to use [] operators in succession an a multi-dimensional array, then each [] must return a (one less)-dimensional array. Then a MArray<SomeType, n>::operator [] must return a MArray<SomeType, n-1>.


1 Answers

The operator[] is evaluated from left to right in obj[1][1]...[1], so obj[1] returns a B object. Suppose now you just have int res = obj[1], then you'll assign to a B object (or C object in the case of multiple invocations of []) an int, but there is no conversion from B or C to int. You probably need to write a conversion operator, like

operator int()
{
   // convert to int here
}

for A, B and C, as overloaded operators are not inherited.

I got rid of your compiling error just by writing such operators for A and B (of course I have linking errors since there are un-defined functions).

Also, note that if you want to write something like obj[1][1]...[1] = 10, you need to overload operator=, as again there is no implicit conversion from int to A or your proxy objects.

Hope this makes sense.

PS: see also @Oncaphillis' comment!

like image 199
vsoftco Avatar answered Oct 13 '22 21:10

vsoftco