operator []

G

Ganesh

Why does operator[] not allowed to take more than one argument? Is
there a workaround for this? I need to seed more than one argument to
[], defined in my own class.

Ganesh
 
E

E. Robert Tisdale

Ganesh said:
Why does operator[] not allowed to take more than one argument?

Because it is the *subscript* operator.
Is there a workaround for this?

Most people substitute operator().
I need to seed more than one argument to operator[], defined in my own class.

It takes exactly *one* argument.
That argument may be an object which contains more than one argument
or an object of a type that defines operator,
so that you can use a comma separated list to construct that object.
 
X

xuatla

Ganesh said:
Why does operator[] not allowed to take more than one argument? Is
there a workaround for this? I need to seed more than one argument to
[], defined in my own class.

Ganesh
I don't know the answer of your question.
But you may try 'opeartor ()' for more than one arguments.

-X
 
A

Alf P. Steinbach

* Ganesh:
Why does operator[] not allowed to take more than one argument?

When the great Dog created the universe, Earth, Norway, etc., not to mention
Denmark, Bell Labs and C++, the Dog wisely decreed that square arguments
should forever be single. Only nice, round arguments would be permitted to
mingle with their fellows and procreate. Ours is not to reason why, but
happily it's easy to communicate with the great Dog: just post your question
in [comp.std.c++], and the Dog will answer -- in Dog language, of course.

Is there a workaround for this?

list/tuple argument
that operator[] reduces the dimensionality by 1
operator()
member function 'at'
proxy result


I need to seed more than one argument to
[], defined in my own class.

Said the great Dog: think not of your own needs, but of the needs of C++!
 
D

David White

Ganesh said:
Why does operator[] not allowed to take more than one argument?

It's a binary operator, so it does take more than one argument, but the
object for which it is called is implicitly the first argument, e.g.,
class MyIntArray
{
public:
MyIntArray(int size);
int &operator[](int index)
{
return array[index];
}
private:
int *array;
};

int main()
{
MyIntArray mia(10);
mia[3] = 7;
}
Is
there a workaround for this? I need to seed more than one argument to
[], defined in my own class.

DW
 
G

Greg

E. Robert Tisdale said:
Ganesh said:
Why does operator[] not allowed to take more than one argument?

Because it is the *subscript* operator.
Is there a workaround for this?

Most people substitute operator().
I need to seed more than one argument to operator[], defined in my own class.

It takes exactly *one* argument.
That argument may be an object which contains more than one argument
or an object of a type that defines operator,
so that you can use a comma separated list to construct that object.

Not to mention that an overloaded operator[] for an object could return
a reference to the object. In other words:

class MyClass
{
public:
...
MyClass& operator[]( int /* or whatever type you like */)
{
...
return *this;
}
};

Thereby allowing expressions of the form:

MyClass c;

c[5][4][3][2][1];

Sure, one would have to be a little crazy to find this a reasonable way
to overload the [] operator. But there's nothing wrong with having a
little fun now and then.

Greg
 
A

Aleksey Loginov

Ganesh said:
Why does operator[] not allowed to take more than one argument? Is
there a workaround for this? I need to seed more than one argument to
[], defined in my own class.

Ganesh

you don't want this. use "operator ()".

struct Test
{
int data;
Test () : data (1) { }

struct SubScript {
Test & ref;
int i;

SubScript ( Test &x, int i ) : ref(x), i(i) { }
int operator [] ( int j ) { return (i==j) ? ref.data : 0; }
};

SubScript operator [] ( int i ) { return SubScript (*this,i); }
};
 

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
474,293
Messages
2,571,506
Members
48,193
Latest member
DannyRober

Latest Threads

Top