shared_array const member function

Can any body please tell me why operator[] is defined in case of shared_array<T> like this way: Type & operator[](std::ptrdiff_t) const; It reports compile time error. Consider the code: class Widget { std::string classId; public: std::string getClassId() const { return classId; } }; int main() { boost::shared_array<Widget> obj(new Widget[10]); std::string id = obj[7].getClassId (); // Error } In my opinion operator function should be defined as follows: const Type & operator[](std::ptrdiff_t) const; Type & operator[](std::ptrdiff_t) ; Please clear my consfusion. Thanks in advance.

AMDG Daya S. Prasad wrote:
Can any body please tell me why operator[] is defined in case of shared_array<T> like this way:
Type & operator[](std::ptrdiff_t) const;
It reports compile time error. Consider the code:
class Widget { std::string classId; public: std::string getClassId() const { return classId; } };
int main() { boost::shared_array<Widget> obj(new Widget[10]); std::string id = obj[7].getClassId (); // Error }
? It compiles fine for me.
In my opinion operator function should be defined as follows:
const Type & operator[](std::ptrdiff_t) const; Type & operator[](std::ptrdiff_t) ;
shared_array is defined like a pointer. int* const array = new int[5]; int& i = array[2]; similarly const boost::shared_array<int> array(new int[5]); int& i = array[2]; In Christ, Steven Watanabe
participants (2)
-
Daya S. Prasad
-
Steven Watanabe