开发者

Clarification on Operators

I've been reading up on class operators and came across the following example:

class Array {
public:
     int& operator[] (unsigned i) { if (i > 99) error(); return data[i]; }
private:
     int data[100];
};

Does this mean I can replace [ and ] with whatever I want? For example, could I use parentheses?

Also, what is the significance of the ampersand in int& operator[]?


On a slightly less importan开发者_StackOverflow社区t note, would it be syntactically correct to use int& operator [] instead of int& operator[]?


Does this mean I can replace [ and ] with whatever I want?

Nopes! Not all operators can be overloaded.

For example, could I use parentheses?

You can but you shouldn't.

Also, what is the significance of the ampersand in int& operator[]?

It means you are returning a reference to an int variable.

EDIT: On a slightly less important note, would it be syntactically correct to use int& operator [] instead of int& operator[]?

There is no difference between the two.


What you are doing is defining an Array class that wrapes a simple integer array. By overloading the [] operator you can acces each element of the array as with any other array, the advantage here is that your are checking superior limit to avoid buffer overflow. The ampersand means that you are returning a reference to the array element, it allows you both assigment a value and getting a value. In c++ when you overload the [] operator dont forget both const and non-const versions:

int& operator[] (conts int a) {...}
int operator[] (conts int a) {...} const


You could use parentheses, because there also is an

operator()

and some libraries (eg. boost::blas) do this. BUT: You cannot make up new parenthesis, because there is no operator{} or operator<>

RE your syntax question: Haven't tried, but don't think so, because operator[] is the name of a function, and you shouldn't add whitespace to a function name.


While you can mostly define operators as you like, parens would be a problem. The reason is conflict with the classes constructor! Array(25), what does it mean? I'm not sure if this is allowed (though I'm quite sure someone else will know), but the point is that even if you are allowed you are going to have a problem with doing that.

In general - symbols that serve as operators can be overloaded/redefined You can specify how ==, &, > and [] work for your class. You can't decide the letter 'q' now means some type of comparison. As far as the () issue, im not sure.

The reference mark & is necessary for indexing because you want to potentially do assignment on the address returned, not just got a value. If you permit the lax description, the "majority' of your operators will not require this.


1) NO there is a fixed set of available 'overloadable' operators in C++.
See this table in wikipedia:
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Table
There is an 'Overloadable' Column in each table

2) The int& indicates that you're returning a Reference to the value.
References in C++ are similar to pointers and worth their own discussion, but in the case of the operator here what it allows if for you to use this operator as the left hand operand of an assignment e.g.

x[3] = 0;

This wouldn't work if it just returned an int;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜