473,378 Members | 1,482 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Two reference functions

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
4 1349
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: David Fisher | last post by:
I have been encouraged by someone else to use a reference rather than a pointer as an "out" parameter to a function, eg. void doStuff(Thing &out1, Thing &out2) as opposed to: void...
8
by: Sreenivas | last post by:
Hi, We cannot return a reference to an automatic variable from a function, as per the ANSI C++ standard the behaviour is undefined. Does this hold for inline functions too? or can I return a...
41
by: Berk Birand | last post by:
Hi, I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by...
5
by: Neal Coombes | last post by:
Posted to comp.lang.c++.moderated with little response. Hoping for better from the unmoderated groups: -------- Original Message -------- Subject: Return appropriately by value, (smart)...
10
by: PCHOME | last post by:
Hi! Would someone please help me thess C error(in gcc on Linux)? The compiler continues to give me: readLP.o: In function `Input_Problem': readLP.o(.text+0x0): multiple definition of...
5
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget...
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
68
by: Jim Langston | last post by:
I remember there was a thread a while back that was talking about using the return value of a function as a reference where I had thought the reference would become invalidated because it was a...
0
markrawlingson
by: markrawlingson | last post by:
Hello, So I've got a list of functions written in ASP Classic which I include into every page within my application(s). This file is getting quite big and in most scenarios needs to be called 4-5...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.