473,396 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Overload operator

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?

May 16 '07 #1
3 1857
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
May 16 '07 #2
On 16 Maj, 15:10, ek <eksa...@yahoo.comwrote:
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

May 16 '07 #3
On May 16, 3:10 pm, ek <eksa...@yahoo.comwrote:
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: ja*********@gmail.com
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
May 16 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: bsaucer | last post by:
I am creating a class with operator overloads. It makes references to another class I've created, but do not wish to modify. I try to overload an operator having arguments having the other class...
7
by: Piotre Ugrumov | last post by:
I have tried to implement the overload of these 2 operators. ostream & operator<<(ostream &out, Person &p){ out<<p.getName()<<" "<<p.getSurname()<<", "<<p.getDateOfBirth()<<endl; return out; }...
1
by: Piotre Ugrumov | last post by:
I'm following your help. I have written the overload of the operator <<. This overload work! :-) But I have some problem with the overload of the operator >>. I have written the overload of this...
3
by: Piotre Ugrumov | last post by:
I have done the overload on the operator >> and << in the class Attore. These 2 overload work correctly. I have done the overload of the same overload in the class Film. The class film ha inside...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
4
by: Chiller | last post by:
Ok, thanks to some good assistance/advice from people in this group I've been able to further develop my Distance class. Since previous posts I've refined my code to accept the unit measurement...
7
by: Sean | last post by:
Can someone help me see why the following "operator=" overloading doesn't work under g++? and the error message is copied here. I see no reason the compiler complain this. Thanks, $ g++...
17
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: ...
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
3
by: i3x171um | last post by:
To start off, I'm using GCC4. Specifically, the MingW (setjmp/longjmp) build of GCC 4.2.1 on Windows XP x64. I'm writing a class that abstracts a message, which can be either an integer (stored as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.