5

I am building an type to be able to manage matrixes so i have searched how to make an [][] operator but no luck so any idea how to do that i just need a way to make double operator this the class am building

#include<iostream>
#include<conio.h>

using namespace std;

class ddouble{
private:
unsigned short int  x, y;
public:
ddouble();
ddouble(unsigned short int, unsigned short int);
double **M;
void read();
void print();
};

ddouble::ddouble(unsigned short int m, unsigned short int n){
for (int i = 0; i < m; i++){
    M = new (nothrow) double *[i];
    for (int I = 0; I < n; I++){
        M[i] = new (nothrow) double[I];
    }
}
}
void ddouble::read(){
for (int i = 0; i < x; i++){

    cout << "plz enter line \n";

    for (int I = 0; I < y; I++){
        cin >> M[i][I];
    }
}
}
void ddouble::print(){

cout << "i,j\t|\t";

for (int i = 0; i < y; i++){
    cout << i << "\t";
}

cout << endl;

for (int i = 0; i < x; i++){

    cout << i << "\t|\t";

    for (int I = 0; I < y; I++){
        cout << M[i][I] << "\t";
    }

    cout << endl;
}
} 

void main(){

ddouble a(2, 2);
a.read();
a.print();

_getch();
}
Karim
  • 97
  • 1
  • 9

3 Answers3

6

I would recommend using std::vector<std::vector<double>>. That said, given the structure you have now, you can simply define an operator[] that returns a double *, e.g.:

double * ddouble::operator[](int i) {
    return M[i];
}

You can now access your variable in main as a[i][j].

Also, you are missing a destructor that deallocates the memory; without it you have memory leaks. You should define a destructor as:

void ddouble::~ddouble() {
    for(int I = 0; I < y; I++) {
        delete [] M[I];
    }

    delete [] M;
}
wolfPack88
  • 4,163
  • 4
  • 32
  • 47
  • thnx man it worked :3 – Karim Jan 02 '15 at 14:59
  • @wolfPack88 : Can you please tell Since we already deleted the memory by applying the loop, Why we need to call `delete [] M;` again in the end. – Unbreakable Jan 02 '15 at 15:46
  • 2
    @Unbreakable: `M` is a pointer to a series of pointers, each accessed as `M[I]` in the loop. Each pointer has allocated some memory as well, which is what we are `delete`-ing in the loop. After the loop, where we have `delete`-ed the memory of the pointers, we need to `delete` the pointer pointing to the pointers, or `M`. – wolfPack88 Jan 02 '15 at 15:48
  • so in total we had y+1 pointers. Kind of.. – Unbreakable Jan 02 '15 at 15:50
5

Personally, I wouldn't. I'd take the simple option, and provide a function (or possible operator()) that takes two arguments.

If you really want to support [][] syntax, then you'll need the first invocation to return some proxy type which also overloads operator[] to give the final result. This would be along the lines of

class proxy {
    double * p;
public:
    proxy(double * p) : p(p) {}
    double & operator[](size_t i) {return p[i];}
};

proxy operator[](size_t i) {return proxy(M[i]);}

Adding the necessary overloads for const-correctness is left as an exercise.

In this simple case, the proxy could simply be a double* pointer; you'd need a class if access were more complicated, or if you wanted to add bounds checking or other access controls.

(And as always when you're writing a resource management class, don't forget the Rule of Three).

Community
  • 1
  • 1
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
2

You can implement proxy class to provide that behavior. Rough sketch would be something like:-

template<class T>
class Array2D 
{
  public:
    class Array1D 
    {
      public:
        T& operator[](int index);
        const T& operator[](int index) const;
        ...
    };
    Array1D operator[](int index);
    const Array1D operator[](int index) const;
    ...
};

Clients of this class are not aware of internal proxy object. I would recommend you go for std::vector<std::vector<double> >. But there are some cases where you want to roll up your own class to provide specific behavior for client requirements such as bound checking etc...

ravi
  • 10,994
  • 1
  • 18
  • 36
  • I downvoted this because 1- you use this snipped from More Effective C++ by Scott Meyers, and you didn't make any reference or something. 2 - This "rough sketch" does not what OP wants. –  Mar 28 '20 at 19:06