0

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?

2
  • 5
    That seems weird; stack[2] should indeed give you the third Apple object. You should post your code - preferably, a simplified version that only includes the code that is necessary to demonstrate the problem. Commented Aug 11, 2012 at 18:49
  • oh, ok, that was another problem. The reason I did not post it is because it's a school assignment, and pretty big assignment basing on prevoius assignments we built upon. Thanks for the big offer for help :D Commented Aug 11, 2012 at 19:04

3 Answers 3

1

You could use a pointer to get array elements:

*(stack+2)[3]; // lookup element 2 and call its [] operator with 3
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.