By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,375 Members | 3,030 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,375 IT Pros & Developers. It's quick & easy.

Two reference functions

P: n/a
Dear All,

I found there are two functions to reference an element in a
std:vector.

TYPE& operator[]( size_type index );
const TYPE& operator[]( size_type index ) const;

I am wondering why the second is needed. Seems we can always use the
first one to do what we want.

Thanks,

Shuisheng

Sep 21 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
shuisheng <sh*********@yahoo.comwrote:
I found there are two functions to reference an element in a
std:vector.

TYPE& operator[]( size_type index );
const TYPE& operator[]( size_type index ) const;

I am wondering why the second is needed. Seems we can always use the
first one to do what we want.
If the std::vector is const, then you cannot use the first one. Only
const member functions can be called on a const object.

Even if the actual vector itself is not const, it may be "locally"
const, e.g., if it is passed by const reference to a function.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Sep 21 '06 #2

P: n/a
shuisheng posted:
TYPE& operator[]( size_type index );
const TYPE& operator[]( size_type index ) const;

If TYPE is a something like a function pointer, or a pointer to an array, or
anything more complex than a simple type such as "int" or "short*", then the
code is broken, e.g.:

#define TYPE int (*(*)())[5]

If TYPE is a typedef, then it will work OK -- but evidently it's a macro
since you have used ALL CAPS for its name.

--

Frederick Gotham
Sep 21 '06 #3

P: n/a

"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:5F*******************@news.indigo.ie...
shuisheng posted:
> TYPE& operator[]( size_type index );
const TYPE& operator[]( size_type index ) const;


If TYPE is a something like a function pointer, or a pointer to an array,
or
anything more complex than a simple type such as "int" or "short*", then
the
code is broken, e.g.:

#define TYPE int (*(*)())[5]

If TYPE is a typedef, then it will work OK -- but evidently it's a macro
since you have used ALL CAPS for its name.
Frederick,
Perhaps you didn't notice that Shuisheng said this was in std::vector?
I assume those are the actual template function signatures, and that TYPE is
the template parameter for the type of object stored in the vector.

-Howard
Sep 21 '06 #4

P: n/a
Howard posted:
Frederick,
Perhaps you didn't notice that Shuisheng said this was in
std::vector?
I assume those are the actual template function signatures, and that
TYPE is the template parameter for the type of object stored in the
vector.

Yes, that seems likely. However I would be extra sure to remind people to
only use ALL CAPS for macros.

--

Frederick Gotham
Sep 23 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.