Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > const ref vs. pointer

Reply
Thread Tools

const ref vs. pointer

 
 
Salt_Peter
Guest
Posts: n/a
 
      11-03-2006

Andre Kostur wrote:
> "Peter" <(E-Mail Removed)> wrote in news:1162507991.607378.77690
> @m7g2000cwm.googlegroups.com:
>
> >
> > Salt_Peter wrote:
> >
> >> They both serve their purpose although the reference is guarenteed to
> >> be a valid object.

> >
> >
> > there is no such guaranty. It may be implied that there is always a
> > valid object behind but it is entirely possible to have a
> > null-reference returned:

>
> Not without invoking Undefined Behaviour....


thank-you

>
> > const Object &getObject(vodi)
> > {
> > Object *p = 0;
> > return *p;
> > }

>
> ... like dereferencing a NULL pointer.
>
> > const Object &r = getObject();
> >
> > this works fine until somebody tries to derefence the object -- means
> > the initialization of the reference does work.

>
> Nope. There's no guarantee that the initialization of that reference
> "worked". As soon as you invoked Undefined Behaviour, all bets are off.
> Your program could do whatever it wanted.


agreed

 
Reply With Quote
 
 
 
 
Peter
Guest
Posts: n/a
 
      11-15-2006

Andre Kostur wrote:
> >> They both serve their purpose although the reference is guarenteed to
> >> be a valid object.

> >
> >
> > there is no such guaranty. It may be implied that there is always a
> > valid object behind but it is entirely possible to have a
> > null-reference returned:

>
> Not without invoking Undefined Behaviour....
>
> > const Object &getObject(vodi)
> > {
> > Object *p = 0;
> > return *p;
> > }

>
> ... like dereferencing a NULL pointer.



creating a reference is not equal dereferencing a pointer.
Creating a reference is identical to initializing a pointer.


> > const Object &r = getObject();
> >
> > this works fine until somebody tries to derefence the object -- means
> > the initialization of the reference does work.

>
> Nope. There's no guarantee that the initialization of that reference
> "worked". As soon as you invoked Undefined Behaviour, all bets are off.
> Your program could do whatever it wanted.



I think this is pretty much defined just by the fact that
when initializing a reference a check for a valid reference would be
too expensive.
Dereferencing a null reference may be undefined but initializing it is
certainly not.

 
Reply With Quote
 
 
 
 
Andre Kostur
Guest
Posts: n/a
 
      11-15-2006
"Peter" <(E-Mail Removed)> wrote in news:1163624779.810389.126060
@e3g2000cwe.googlegroups.com:

>
> Andre Kostur wrote:
>> >> They both serve their purpose although the reference is guarenteed

to
>> >> be a valid object.
>> >
>> >
>> > there is no such guaranty. It may be implied that there is always a
>> > valid object behind but it is entirely possible to have a
>> > null-reference returned:

>>
>> Not without invoking Undefined Behaviour....
>>
>> > const Object &getObject(vodi)
>> > {
>> > Object *p = 0;
>> > return *p;
>> > }

>>
>> ... like dereferencing a NULL pointer.

>
>
> creating a reference is not equal dereferencing a pointer.
> Creating a reference is identical to initializing a pointer.


"return *p;" That dereferences the p pointer. Undefined Behaviour.

>> > const Object &r = getObject();
>> >
>> > this works fine until somebody tries to derefence the object --

means
>> > the initialization of the reference does work.

>>
>> Nope. There's no guarantee that the initialization of that reference
>> "worked". As soon as you invoked Undefined Behaviour, all bets are

off.
>> Your program could do whatever it wanted.

>
>
> I think this is pretty much defined just by the fact that
> when initializing a reference a check for a valid reference would be
> too expensive.


Irrelevant. The program caused a NULL pointer to be dereferenced. After
that, the program's behaviour is undefined. It may run until the
reference is used, it may crash at the point of the NULL pointer
dereference, it may never crash. Doesn't matter. It's Undefined
Behaviour.

> Dereferencing a null reference may be undefined but initializing it is
> certainly not.


A NULL pointer is dereferenced. Undefined Behaviour. It doesn't matter
what you do with it.
 
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
const pointer versus const content pointed to by pointer Disc Magnet C Programming 1 05-06-2010 10:27 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Smart Pointer release() const : it can set the pointer to null with the keyword "const"? coala C++ 1 09-06-2006 03:00 PM
Smart Pointer release() const : it can set the pointer to null with the keyword "const"? coala C++ 3 09-06-2006 02:58 PM
converting pointer to const ref cppaddict C++ 6 10-14-2003 09:01 AM



Advertisments