Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Returning NULL references...

Reply
Thread Tools

Returning NULL references...

 
 
Tony Di Croce
Guest
Posts: n/a
 
      09-29-2004
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
 
Reply With Quote
 
 
 
 
Moonlit
Guest
Posts: n/a
 
      09-29-2004
Hi,

"Tony Di Croce" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.


 
Reply With Quote
 
 
 
 
Andre Kostur
Guest
Posts: n/a
 
      09-29-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Tony Di Croce) wrote in
news:(E-Mail Removed) 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).
 
Reply With Quote
 
Peter Koch Larsen
Guest
Posts: n/a
 
      09-29-2004

"Tony Di Croce" <(E-Mail Removed)> skrev i en meddelelse
news:(E-Mail Removed) 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


 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      09-29-2004
Andre Kostur <(E-Mail Removed)> wrote in
news:Xns95735C19ECCECnntpspamkosturnet@207.35.177. 134:

> (E-Mail Removed) (Tony Di Croce) wrote in
> news:(E-Mail Removed) 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....
 
Reply With Quote
 
Maitre Bart
Guest
Posts: n/a
 
      09-29-2004

"Andre Kostur" <(E-Mail Removed)> wrote in message
news:Xns95735DBB8BDF9nntpspamkosturnet@207.35.177. 134...
> Andre Kostur <(E-Mail Removed)> wrote in
> news:Xns95735C19ECCECnntpspamkosturnet@207.35.177. 134:
>
> > (E-Mail Removed) (Tony Di Croce) wrote in
> > news:(E-Mail Removed) 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.


 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      09-29-2004
"Maitre Bart" <(E-Mail Removed)> wrote in news:cjesqn$9ac$(E-Mail Removed):

>> >> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
file reader returning null when file is not null content jason Java 13 05-14-2010 01:50 AM
returning none when it should be returning a list? randomtalk@gmail.com Python 11 05-02-2006 10:26 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
HttpURLConnection.getInputStream() is returning null igoR Buttler Java 1 01-07-2004 06:36 PM
OnEditCommand - .FindControl Returning Null George Durzi ASP .Net 0 07-14-2003 09:54 PM



Advertisments