Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Overload operator

Reply
Thread Tools

Overload operator

 
 
ek
Guest
Posts: n/a
 
      05-16-2007
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?

 
Reply With Quote
 
 
 
 
Zeppe
Guest
Posts: n/a
 
      05-16-2007
ek wrote:
> 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
 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      05-16-2007
On 16 Maj, 15:10, ek <(E-Mail Removed)> wrote:
> 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.

--
Erik Wikström

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-16-2007
On May 16, 3:10 pm, ek <(E-Mail Removed)> wrote:
> 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.

--
James Kanze (Gabi Software) email: http://www.velocityreviews.com/forums/(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


 
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
How to handle the lack of operator overload ? Mr Smith Java 7 02-10-2005 08:37 AM
function overload (not operator overload) Ying-Chieh Liao Perl Misc 3 10-11-2004 11:24 AM
How use the overload of>> (or<<) of a class in the overload of << and >> of another class? Piotre Ugrumov C++ 3 01-25-2004 08:08 PM
Q: Overload operator new/delete Jakob Bieling C++ 0 08-08-2003 12:20 PM
overload operator - Matthew Monopole C++ 1 08-08-2003 09:39 AM



Advertisments