Calling operator [] within a class?

Started by
1 comment, last by gimp 22 years, 6 months ago
I have a base class CTree, that exports the operator [] to give access to the children. I''d like a derived class to be able to access the children to call a member on each child. I tried what seems intuitive but it failed: *this.Process(Event); and this (*this[m_i])->Process(Event); Any idea on how to refer to the [] operator of your own class? Many thanks Chris Chris Brodie http:\\fourth.flipcode.com
Chris Brodie
Advertisement
It should be possible to call the operator [] method directly:
operator[] (nIndex).DoSomething (); 

However,
(*this)[nIndex].DoSomething (); 

should work, too.

Sorry, can''t verify it at the moment.
It would be helpful to know what error you are getting, but I think (from past experiences) that it is an operation priority issue.

You have: (*this[m_i])->Process(Event);
Which means that it tries to get the m_i item from the pointer ''this'' and dereference the result.. then call Process with a pointer accessor.

What you are trying to do is dereference a pointer as to get the container object, get the m_i item from it, and call Process on it: (*this)[m_i].Process(Event).

You can always cheat and take the functionality of operator [] out and put it into a method like GetNodeAtIndex. Have operator [] call GetNodeAtIndex, and you can just call that too within the class: GetNodeAtIndex(m_i).Process(Event)... but then again, it''s not as nice


---
PAGE FAULT: Please insert "Swap File, Disk 2"
and press any key to continue.
---
PAGE FAULT: Please insert "Swap File, Disk 2"
and press any key to continue.

This topic is closed to new replies.

Advertisement