Overload operator

E

ek

In the class below I overload the "()" operator. When reading an
element in an int array "()" is therefore used. But why can't I still
use "[]" when writing something like a[2] = 4;

class MyArray {
public:
MyArray(int a) : data(new int[a]){}

~MyArray() {delete[] data;}

int& operator()(int a){
return data[a];
}

int operator()(int a) const{
return data[a];
}

private:
int* data;
};

int main() {

MyArray Arr(4);
Arr[2] = 5; // error: no match for 'operator[]' in 'Arr[2]'

return 0;
}

When overloading "()" the normal "[]" operator no longer works. I can
instead do the assignment with Arr(2) = 5; but I have not specified an
overload of "()" that does assignment, why is that not needed?
 
Z

Zeppe

ek said:
In the class below I overload the "()" operator. When reading an
element in an int array "()" is therefore used. But why can't I still
use "[]" when writing something like a[2] = 4;

you have to define the operator[] to use [].

When overloading "()" the normal "[]" operator no longer works.

I doubt it would work even without operator() if you don't define the
operator[]...

Regards,

Zeppe
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

In the class below I overload the "()" operator. When reading an
element in an int array "()" is therefore used. But why can't I still
use "[]" when writing something like a[2] = 4;

class MyArray {
public:
MyArray(int a) : data(new int[a]){}

~MyArray() {delete[] data;}

int& operator()(int a){
return data[a];
}

int operator()(int a) const{
return data[a];
}

private:
int* data;

};
When overloading "()" the normal "[]" operator no longer works. I can
instead do the assignment with Arr(2) = 5; but I have not specified an
overload of "()" that does assignment, why is that not needed?

This one takes care of the assignment:

int& operator()(int a){
return data[a];
}

It returns a reference to the element so when you assign to the
reference you assign to the element.
 
J

James Kanze

In the class below I overload the "()" operator. When reading an
element in an int array "()" is therefore used. But why can't I still
use "[]" when writing something like a[2] = 4;

You can. The two are completely orthogonal.
class MyArray {
public:
MyArray(int a) : data(new int[a]){}
~MyArray() {delete[] data;}
int& operator()(int a){
return data[a];
}
int operator()(int a) const{
return data[a];
}
private:
int* data;
};
int main() {
MyArray Arr(4);
Arr[2] = 5; // error: no match for 'operator[]' in 'Arr[2]'

That's because you didn't define one.
return 0;

}
When overloading "()" the normal "[]" operator no longer works.

What "normal []"? A class doesn't support "[]" unless you
define it.
I can
instead do the assignment with Arr(2) = 5; but I have not specified an
overload of "()" that does assignment, why is that not needed?

The non-const version of your operator() returns a reference.
In C++ parlance, that means that the expression is an lvalue; in
this case, an lvalue of type int. So you can assign to it.
Since the reference actually refers to data[a], that's what gets
assigned to.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top