Reputation: 423
I created this class, say, Apple
, and it has an overloaded operator []
.
Then in another class, I made an array of apples, Apple stack[10]
.
Then when I was using stack[2]
in an expression, instead of stack[2]
being an object, the compiler is acting like it sees stack as stack[0]
, and then calling the operator []
and its parameter as 2
.
Could you suggest a way I could calling stack[2]
without being that way?
Upvotes: 0
Views: 111
Reputation: 1352
Providing the code would have been helpful.
However, suppose you have a variable x
in the class Apple. You can access it using (stack+2)->x
. This would work same as stack[2].x
. And in case you want to use your overloaded operator, you may use it similarly. This wont confuse the compiler.
Upvotes: 0
Reputation: 83527
Based on the information given so far, stack[2]
refers to an Apple
object. You can also do stack[2][5]
to call the overloaded operator[]
in your Apple
class.
Upvotes: 1
Reputation: 824
You could use a pointer to get array elements:
*(stack+2)[3]; // lookup element 2 and call its [] operator with 3
Upvotes: 1