473,326 Members | 2,337 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,326 software developers and data experts.

Returning NULL references...

Given:

T& operator [] ( int index ); // T is a parameterized type

What should be returned when index is out of range?

This same question comes up virtually everywhere I return a reference.
I need to return a reference (as opposed to a copy) because I want to
use this operator as an l-value sometimes...

Does anyone know what does the STL does?

Tony
Jul 22 '05 #1
6 1472
Hi,

"Tony Di Croce" <di*****@gmail.com> wrote in message
news:37**************************@posting.google.c om...
Given:

T& operator [] ( int index ); // T is a parameterized type

What should be returned when index is out of range?

This same question comes up virtually everywhere I return a reference.
I need to return a reference (as opposed to a copy) because I want to
use this operator as an l-value sometimes...

Does anyone know what does the STL does? Yes, it creates an entry to the map and returns a reference to it (that's
why [] isn't constant).

Tony


Do the same as the STL or throw an exception or return a reference to for
instance a local (to the class) variable.

Of course just adding an entry will keep yourself and people reading your
code, from any surprises since that is what people expect.

Regards, Ron AF Greve.
Jul 22 '05 #2

"Tony Di Croce" <di*****@gmail.com> skrev i en meddelelse
news:37**************************@posting.google.c om...
Given:

T& operator [] ( int index ); // T is a parameterized type

What should be returned when index is out of range?
Is it something like a std::vector? In that case, i would terminate the
program or possibly throw - but that is a design decision, of course.

This same question comes up virtually everywhere I return a reference.
I need to return a reference (as opposed to a copy) because I want to
use this operator as an l-value sometimes...
Sounds strange! Most of the time i return references, i will have a variable
to return.

Does anyone know what does the STL does?
For a set, the element is created with the value T(). For a std::vector,
behaviour is undefined - an implementation can do what it likes.

Tony


/Peter
Jul 22 '05 #3
di*****@gmail.com (Tony Di Croce) wrote in
news:37**************************@posting.google.c om:
Given:

T& operator [] ( int index ); // T is a parameterized type

What should be returned when index is out of range?
An exception. Or leave it as undefined behaviour.
This same question comes up virtually everywhere I return a reference.
I need to return a reference (as opposed to a copy) because I want to
use this operator as an l-value sometimes...

Does anyone know what does the STL does?


Undefined behaviour. One of the strong points of references is that they
_cannot_ be "NULL" (at least not without invoking undefined behaviour).
Jul 22 '05 #4
Andre Kostur <nn******@kostur.net> wrote in
news:Xn*******************************@207.35.177. 134:
di*****@gmail.com (Tony Di Croce) wrote in
news:37**************************@posting.google.c om:
Given:

T& operator [] ( int index ); // T is a parameterized type

What should be returned when index is out of range?


An exception. Or leave it as undefined behaviour.
This same question comes up virtually everywhere I return a
reference. I need to return a reference (as opposed to a copy)
because I want to use this operator as an l-value sometimes...

Does anyone know what does the STL does?


Undefined behaviour. One of the strong points of references is that
they _cannot_ be "NULL" (at least not without invoking undefined
behaviour).


Oops.. I'm thinking specifically of vectors. For map and set that would
create a default-constructed object at that index and return a reference to
that....
Jul 22 '05 #5

"Andre Kostur" <nn******@kostur.net> wrote in message
news:Xn*******************************@207.35.177. 134...
Andre Kostur <nn******@kostur.net> wrote in
news:Xn*******************************@207.35.177. 134:
di*****@gmail.com (Tony Di Croce) wrote in
news:37**************************@posting.google.c om:
Given:

T& operator [] ( int index ); // T is a parameterized type

What should be returned when index is out of range?
An exception. Or leave it as undefined behaviour.
This same question comes up virtually everywhere I return a
reference. I need to return a reference (as opposed to a copy)
because I want to use this operator as an l-value sometimes...

Does anyone know what does the STL does?


Undefined behaviour. One of the strong points of references is that
they _cannot_ be "NULL" (at least not without invoking undefined
behaviour).


Oops.. I'm thinking specifically of vectors. For map and set that would
create a default-constructed object at that index and return a reference

to that....


You may as well increase your vector size up to the specified index.
That way you would get about the same behavior you just described for map
and set.
Anyway, if the vector is not able to allocate, it will result in an
axception of some sort.
So better be prepared for an exception anyway.
Jul 22 '05 #6
"Maitre Bart" <le*****@cae.com> wrote in news:cj**********@dns3.cae.ca:
>> This same question comes up virtually everywhere I return a
>> reference. I need to return a reference (as opposed to a copy)
>> because I want to use this operator as an l-value sometimes...
>>
>> Does anyone know what does the STL does?
>
> Undefined behaviour. One of the strong points of references is that
> they _cannot_ be "NULL" (at least not without invoking undefined
> behaviour).
Oops.. I'm thinking specifically of vectors. For map and set that would create a default-constructed object at that index and return a

reference to
that....
You may as well increase your vector size up to the specified index.


Up to what size? You may not know the maximum index before you start.
As a result, you'll have to pay for the construction of all of these
extra objects that you may not ever use....
That way you would get about the same behavior you just described for map and set.
Not really. With map and set, you won't have a bunch of extra objects
being constructed just in case...
Anyway, if the vector is not able to allocate, it will result in an
axception of some sort.
So better be prepared for an exception anyway.

Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: {RainmakeR} | last post by:
Hi all, I've noticed something strange with returning references, both in PHP4 and 5 - maybe it's supposed to do this, I don't know - but it seems a bit weird nonetheless. Say we have the...
1
by: John Hunter | last post by:
I am writing a python extension module and have a reference counting question My function looks like static PyObject * pokereval_seven_cards(PyObject *self, PyObject *args) { int i;
7
by: Pablo J Royo | last post by:
Hello: i have a function that reads a file as an argument and returns a reference to an object that contains some information obtained from the file: FData &ReadFile(string FilePath); But ,...
1
by: Justin | last post by:
Hi, In the process of localizing the 'regions' table, we added three new tables. The localized data will be stored in the TokenKeys and TokenValues tables. It would be easier if we did away with...
18
by: cppaddict | last post by:
Hi, Is it considered bad form to have the subscript operator return a const reference variable? If not, what is the proper way to do it? My question was prompted by the code below, my...
19
by: Steven T. Hatton | last post by:
I believe it is technically possible to return a pointer to the first element of an array. I can persuade the returned pointer to act like an array, with some qualifications. I'm specifically...
4
by: Jon Skeet | last post by:
I've just noticed something rather odd and disturbing. The following code displays "True": using System; class Test { public static void Main(string args) { string x = new string...
21
by: Jim Langston | last post by:
I'm sure this has been asked a few times, but I'm still not sure. I want to create a function to simplify getting a reference to a CMap in a map. This is what I do now in code: ...
2
by: Joe | last post by:
I'm binding to a column in a TemplateField. In some cases the join I have returns a null for an int field. I would like to specify a default value somehow so I don't end up with a null exception. ...
23
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.