454,603 Members | 1,461 Online Need help? Post your question and get tips & solutions from a community of 454,603 IT Pros & Developers. It's quick & easy.

 P: n/a Hello All, Once again another question. :) This time I would like to understand why one needs to overload the [ ] operator to access objects in array format? Example code: class A { { }; class B { private: A array; A* pA = array; public: A& operator [ ] (int i) { return pA[i]; } //Line 1: void do_something(void) { A a; a = pA; } //Line 2 }; Why do I need Line 1? Since pA is a pointer of type A, can I not directly access "array members" using simple pointer arithmetic? i.e. without declaring Line 1, use Line 2 or equivalently use the below statement to replace Line 2 a = *(pA + 2); I'm sure I'm missing something. Can someone please point out my misconception? Thanks again in advance! Kush Nov 13 '06 #1
3 Replies

 P: n/a Kush wrote in message <11**********************@m7g2000cwm.googlegroups. com>... >Hello All,Once again another question. :)This time I would like to understand why one needs to overload the [ ]operator to access objects in array format?Example code:class A { };class B {private:A array;A* pA = array;public:A& operator [ ] (int i) { return pA[i]; } //Line 1:void do_something(void) { A a; a = pA; } //Line 2 };Why do I need Line 1? Since pA is a pointer of type A, can I notdirectly access "array members" using simple pointer arithmetic?i.e. without declaring Line 1, use Line 2or equivalently use the below statement to replace Line 2a = *(pA + 2);I'm sure I'm missing something. Can someone please point out mymisconception?Thanks again in advance!Kush One good reason might be: to check the index for range. A& operator [ ] ( int i ) { if( i < 0 || i 9 ){ throw std::out_of_range();} // or 'assert', or ? return pA[ i ]; } //Line 1: -- Bob R POVrookie Nov 13 '06 #2

 P: n/a Kush wrote: Hello All, Once again another question. :) This time I would like to understand why one needs to overload the [ ] operator to access objects in array format? Example code: class A { { }; class B { private: A array; A* pA = array; public: A& operator [ ] (int i) { return pA[i]; } //Line 1: void do_something(void) { A a; a = pA; } //Line 2 }; Why do I need Line 1? Since pA is a pointer of type A, can I not directly access "array members" using simple pointer arithmetic? Line 1 is there to access the array from the outside world. You don't need the pointer pA at all. array already decays to a pointer anyways. > i.e. without declaring Line 1, use Line 2 or equivalently use the below statement to replace Line 2 a = *(pA + 2); There is no need: A a = array; will do Try something like this, although a std::vector would be more usefull: #include #include class A { }; template< typename T , const size_t Size > class B { T array[Size]; public: A& operator [ ] (size_t i) { if(i < 0 || i Size - 1) throw std::out_of_range("array out of bounds"); return array[i]; } void do_something() { T a; a = array; } }; int main() { try { B< A, 10 b; // A a = b; // throws b.do_something(); } catch( const std::exception& r_e ) { std::cerr << "Error: "; std::cerr << r_e.what() << std::endl; } } ___ Note that a std::vector has an at(...) member function that does the range_check for you. Its so much easier to do the above with a vector. #include #include #include template< typename T > class B { std::vector< T vt; public: B(size_t size, const T& t = T()) : vt(size, t) { } // ctor T& operator[](size_t i) { return vt.at(i); } void do_something() { vt.at(2) = 33.3; } }; int main() { try { B< double b( 5, 11.1 ); // A a = b; // throws b.do_something(); for(size_t i = 0; i < 10; ++i) { std::cout << "b[" << i << "] = "; std::cout << b[i] << std::endl; } } catch( const std::exception& r_e ) { std::cerr << "Error: "; std::cerr << r_e.what() << std::endl; } } /* b = 11.1 b = 11.1 b = 33.3 b = 11.1 b = 11.1 */ Nov 13 '06 #3

 P: n/a Kush: This time I would like to understand why one needs to overload the [ ] operator to access objects in array format? The same reason why you have to eat with a spoon: You don't, but everyone's doing it. Example code: class A { { }; class B { private: A array; A* pA = array; public: A& operator [ ] (int i) { return pA[i]; } //Line 1: void do_something(void) { A a; a = pA; } //Line 2 }; Why do I need Line 1? Since pA is a pointer of type A, can I not directly access "array members" using simple pointer arithmetic? Within the class, yes you can. The operator[] you have provided is so that outside of the class, people can do: B obj; obj = ... ; i.e. without declaring Line 1, use Line 2 or equivalently use the below statement to replace Line 2 a = *(pA + 2); Both in C and in C++, the following: arr[i] is equivalent to: *(arr+i) which is equivalent to: *(i+arr) which is equivalent to: i[arr] -- Frederick Gotham Nov 13 '06 #4

### This discussion thread is closed

Replies have been disabled for this discussion. 