Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > overloading [][]

Reply
Thread Tools

overloading [][]

 
 
Andre
Guest
Posts: n/a
 
      08-24-2003
Hi,

I have a class which contains a two-dimensional array. I need to
overload something like the [][] operator so that I'm able to access the
internal array elements via the class object. For example:

Matrix a = new Matrix(10,10);

a[1][1] = 6;

Is this possible? How can I simulate a two dimensional array using a
class in c++? Thanks

-Andre

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      08-24-2003

"Andre" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> I have a class which contains a two-dimensional array. I need to
> overload something like the [][] operator so that I'm able to access the
> internal array elements via the class object. For example:
>
> Matrix a = new Matrix(10,10);
>
> a[1][1] = 6;
>
> Is this possible? How can I simulate a two dimensional array using a
> class in c++? Thanks
>
> -Andre
>


There is no operator[][], there is only the operator[]. You can do what you
want with a proxy class, something like this

class Proxy
{
public:
double operator[](int j) const;
double& operator[](int j);
};

class Matrix
{
public:
const Proxy operator[](int i) const;
Proxy operator[](int i);
};

I hope you get the idea, you overload operator[] on your Matrix object to
return another class (the Proxy class), you then overload operator[] on the
proxy class to return a Matrix element.

john


 
Reply With Quote
 
 
 
 
Andre
Guest
Posts: n/a
 
      08-24-2003
Neat thatnks, I get the idea.

cheers

-Andre

John Harrison wrote:

> "Andre" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>Hi,
>>
>>I have a class which contains a two-dimensional array. I need to
>>overload something like the [][] operator so that I'm able to access the
>>internal array elements via the class object. For example:
>>
>>Matrix a = new Matrix(10,10);
>>
>>a[1][1] = 6;
>>
>>Is this possible? How can I simulate a two dimensional array using a
>>class in c++? Thanks
>>
>>-Andre
>>

>
>
> There is no operator[][], there is only the operator[]. You can do what you
> want with a proxy class, something like this
>
> class Proxy
> {
> public:
> double operator[](int j) const;
> double& operator[](int j);
> };
>
> class Matrix
> {
> public:
> const Proxy operator[](int i) const;
> Proxy operator[](int i);
> };
>
> I hope you get the idea, you overload operator[] on your Matrix object to
> return another class (the Proxy class), you then overload operator[] on the
> proxy class to return a Matrix element.
>
> john
>
>


 
Reply With Quote
 
Sim
Guest
Posts: n/a
 
      08-24-2003
Just wondering.. what does they "const" in front of a function do:

double operator[](int j) const;

Thanks

Sim


John Harrison wrote:

> "Andre" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>Hi,
>>
>>I have a class which contains a two-dimensional array. I need to
>>overload something like the [][] operator so that I'm able to access the
>>internal array elements via the class object. For example:
>>
>>Matrix a = new Matrix(10,10);
>>
>>a[1][1] = 6;
>>
>>Is this possible? How can I simulate a two dimensional array using a
>>class in c++? Thanks
>>
>>-Andre
>>

>
>
> There is no operator[][], there is only the operator[]. You can do what you
> want with a proxy class, something like this
>
> class Proxy
> {
> public:
> double operator[](int j) const;
> double& operator[](int j);
> };
>
> class Matrix
> {
> public:
> const Proxy operator[](int i) const;
> Proxy operator[](int i);
> };
>
> I hope you get the idea, you overload operator[] on your Matrix object to
> return another class (the Proxy class), you then overload operator[] on the
> proxy class to return a Matrix element.
>
> john
>
>


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      08-24-2003

"Sim" <(E-Mail Removed)> wrote in message
news:3f49163f$(E-Mail Removed)...
> Just wondering.. what does they "const" in front of a function do:
>
> double operator[](int j) const;
>
> Thanks
>
> Sim
>


It means that the function does not modify the object and therefore that the
function can be called on a const object.

E.g.

struct X
{
void f();
void g() const;
};

const X x;
x.f(); // error
x.g(); // ok

Functions which do not modify objects should almost always be declared
const. const objects don't get used much, but const references are very
common.

john


 
Reply With Quote
 
Immanuel Albrecht
Guest
Posts: n/a
 
      08-24-2003
"John Harrison" <(E-Mail Removed)> wrote in
news:bib1jv$798kd$(E-Mail Removed)-berlin.de:

>
> "Andre" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Hi,
>>
>> I have a class which contains a two-dimensional array. I need to
>> overload something like the [][] operator so that I'm able to access
>> the internal array elements via the class object. For example:
>>
>> Matrix a = new Matrix(10,10);
>>
>> a[1][1] = 6;
>>
>> Is this possible? How can I simulate a two dimensional array using a
>> class in c++? Thanks
>>
>> -Andre
>>

>
> There is no operator[][], there is only the operator[]. You can do
> what you want with a proxy class, something like this
>
> class Proxy
> {
> public:
> double operator[](int j) const;

const double&
> double& operator[](int j);
> };
>
> class Matrix
> {
> public:
> const Proxy operator[](int i) const;

^^^^^
const Proxy & and
> Proxy operator[](int i);

Proxy& will have better performance, plus, you can have the
proxyclass save a reference to your data matrix-row and I would make this
class inside the Matrix namespace to avoid name collisions with other
Proxy classes for different multi-dimensional arrays.
> };


References combined with inline functions will have improved performance,
since there will be no copy-constructor and destructor calls, less
overhead.
 
Reply With Quote
 
Sim
Guest
Posts: n/a
 
      08-24-2003
Thanks. That clears it

Sim

Gianni Mariani wrote:
> Sim wrote:
>
>> Just wondering.. what does they "const" in front of a function do:
>>
>> double operator[](int j) const;

>
>
> It declares that the method does not alter any "non mutable" object state.
>
> e.g.
>
> struct A {
> int a;
> mutable int b;
> void doer_a() const
> {
> a = 3; // illegal;
> }
> void doer_b() const
> {
> b = 3; // legal;
> }
> };
>


 
Reply With Quote
 
Sim
Guest
Posts: n/a
 
      08-24-2003
The topic seemed interesting so I gave it a try myself. I can't get my
proxy class running. I get a number of errors when I do something like:

matrixA[0][0] = 5.0;

or std::cout << matrixA[0][0];

I simply did a:

const Proxy operator[](int i) const {
return this->proxy[i];
}

Proxy operator[](int i) {
return this->proxy[i];
}

and in proxy I did:

double operator[](int j) const {
return matrix->[j];
}
double& operator[](int j) {
return matrix->[j];
}

and I had my two-dim array in the proxy class:

double **matrix;


What am I doing wrong? Andre if you got it working could you help me out?

thanks

Sim


Immanuel Albrecht wrote:

> "John Harrison" <(E-Mail Removed)> wrote in
> news:bib1jv$798kd$(E-Mail Removed)-berlin.de:
>
>
>>"Andre" <(E-Mail Removed)> wrote in message
>>news:(E-Mail Removed)...
>>
>>>Hi,
>>>
>>>I have a class which contains a two-dimensional array. I need to
>>>overload something like the [][] operator so that I'm able to access
>>>the internal array elements via the class object. For example:
>>>
>>>Matrix a = new Matrix(10,10);
>>>
>>>a[1][1] = 6;
>>>
>>>Is this possible? How can I simulate a two dimensional array using a
>>>class in c++? Thanks
>>>
>>>-Andre
>>>

>>
>>There is no operator[][], there is only the operator[]. You can do
>>what you want with a proxy class, something like this
>>
>>class Proxy
>>{
>>public:
>> double operator[](int j) const;

>
> const double&
>
>> double& operator[](int j);
>>};
>>
>>class Matrix
>>{
>>public:
>> const Proxy operator[](int i) const;

>
> ^^^^^
> const Proxy & and
>
>> Proxy operator[](int i);

>
> Proxy& will have better performance, plus, you can have the
> proxyclass save a reference to your data matrix-row and I would make this
> class inside the Matrix namespace to avoid name collisions with other
> Proxy classes for different multi-dimensional arrays.
>
>>};

>
>
> References combined with inline functions will have improved performance,
> since there will be no copy-constructor and destructor calls, less
> overhead.


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      08-24-2003

"Immanuel Albrecht" <(E-Mail Removed)> wrote in message
news:bib6g1$l5c$03$(E-Mail Removed)-online.com...
> "John Harrison" <(E-Mail Removed)> wrote in
> news:bib1jv$798kd$(E-Mail Removed)-berlin.de:
>
> >
> > "Andre" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> >> Hi,
> >>
> >> I have a class which contains a two-dimensional array. I need to
> >> overload something like the [][] operator so that I'm able to access
> >> the internal array elements via the class object. For example:
> >>
> >> Matrix a = new Matrix(10,10);
> >>
> >> a[1][1] = 6;
> >>
> >> Is this possible? How can I simulate a two dimensional array using a
> >> class in c++? Thanks
> >>
> >> -Andre
> >>

> >
> > There is no operator[][], there is only the operator[]. You can do
> > what you want with a proxy class, something like this
> >
> > class Proxy
> > {
> > public:
> > double operator[](int j) const;

> const double&
> > double& operator[](int j);
> > };
> >
> > class Matrix
> > {
> > public:
> > const Proxy operator[](int i) const;

> ^^^^^
> const Proxy & and
> > Proxy operator[](int i);

> Proxy& will have better performance,


If you return a reference, then you have to have an object to refer to.
Where do you propose to store that object? I think its well known that this
scheme is impossible to implement correctly and safely. See for instance
Effective C++ by Scott Meyer who gives all the various flawed possibilities
a good going over.

Unless of course you have some code that shows differently ...

john


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      08-24-2003

"Sim" <(E-Mail Removed)> wrote in message
news:3f49202f$(E-Mail Removed)...
> The topic seemed interesting so I gave it a try myself. I can't get my
> proxy class running. I get a number of errors when I do something like:
>
> matrixA[0][0] = 5.0;
>
> or std::cout << matrixA[0][0];
>
> I simply did a:
>
> const Proxy operator[](int i) const {
> return this->proxy[i];
> }
>
> Proxy operator[](int i) {
> return this->proxy[i];
> }
>
> and in proxy I did:
>
> double operator[](int j) const {
> return matrix->[j];
> }
> double& operator[](int j) {
> return matrix->[j];
> }
>
> and I had my two-dim array in the proxy class:
>
> double **matrix;
>
>
> What am I doing wrong? Andre if you got it working could you help me out?
>


Well the two dim array is supposed to be in the Matrix class. The proxy
class just hold a reference to the matrix and the first index.

Something like this (untested code)

class Matrix;

class Proxy
{
friend class Matrix;
double& operator[](int j) { return m.matrix[i][j]; }
private:
Proxy(Matrix& mm, int ii) : m(mm), i(ii) {}
Matrix& m;
int i;
};

class Matrix
{
friend class Proxy;
public:
Proxy operator[](int i) { return Proxy(*this, i); }
private:
double** matrix;
};

Basically the Proxy class just holds the parameters used in the first
operator[] call, so that when the second operator[] call happens they are
available to get the element from the Matrix.

john

> thanks
>
> Sim
>
>
> Immanuel Albrecht wrote:
>
> > "John Harrison" <(E-Mail Removed)> wrote in
> > news:bib1jv$798kd$(E-Mail Removed)-berlin.de:
> >
> >
> >>"Andre" <(E-Mail Removed)> wrote in message
> >>news:(E-Mail Removed)...
> >>
> >>>Hi,
> >>>
> >>>I have a class which contains a two-dimensional array. I need to
> >>>overload something like the [][] operator so that I'm able to access
> >>>the internal array elements via the class object. For example:
> >>>
> >>>Matrix a = new Matrix(10,10);
> >>>
> >>>a[1][1] = 6;
> >>>
> >>>Is this possible? How can I simulate a two dimensional array using a
> >>>class in c++? Thanks
> >>>
> >>>-Andre
> >>>
> >>
> >>There is no operator[][], there is only the operator[]. You can do
> >>what you want with a proxy class, something like this
> >>
> >>class Proxy
> >>{
> >>public:
> >> double operator[](int j) const;

> >
> > const double&
> >
> >> double& operator[](int j);
> >>};
> >>
> >>class Matrix
> >>{
> >>public:
> >> const Proxy operator[](int i) const;

> >
> > ^^^^^
> > const Proxy & and
> >
> >> Proxy operator[](int i);

> >
> > Proxy& will have better performance, plus, you can have the
> > proxyclass save a reference to your data matrix-row and I would make

this
> > class inside the Matrix namespace to avoid name collisions with other
> > Proxy classes for different multi-dimensional arrays.
> >
> >>};

> >
> >
> > References combined with inline functions will have improved

performance,
> > since there will be no copy-constructor and destructor calls, less
> > overhead.

>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: Overloading __init__ & Function overloading Iyer, Prasad C Python 4 09-30-2005 08:01 PM
Re: Overloading __init__ & Function overloading Fredrik Lundh Python 0 09-30-2005 03:59 PM
Overloading __init__ & Function overloading Iyer, Prasad C Python 3 09-30-2005 02:17 PM
Re: Overloading __init__ & Function overloading Steve Holden Python 0 09-30-2005 01:58 PM
Re: Overloading __init__ & Function overloading Fredrik Lundh Python 0 09-30-2005 01:53 PM



Advertisments