tags:

views:

244

answers:

5

I have read from the Wikipedia that:

“References cannot be null, whereas pointers can; every reference refers to some object, although it may or may not be valid.”

But I don’t believe because of following code look at it compiler gives no error:

class person
{
public:
virtual void setage()=0;
};
main()
{
person *object=NULL;
person &object1=*object;
}

Please elaborate this point.

+19  A: 

In your code:

person *object=NULL;
person &object1=*object;

you dereference a NULL pointer, so you get undefined behaviour. And to answer your question, there is no such thing as a NULL reference.

And to address the other part of your question, just because a program compiles, there is no guarantee that it is correct or that it will work. C++ compilers are not required to even attempt to diagnose the kind of error your code contains.

anon
+2  A: 

Well, you can do whatever you want in C++. Another example:

person &object1 = *( reinterpret_cast<person*>(0) );

You are invoking an undefined behavior in the above case, beside the case you mentioned!

AraK
Thanks Neil, I corrected it.
AraK
Is there a reason you prefer `reinterpret_cast` to a `static_cast`? (They behave the same in this case of course.)
avakar
@avakar That's what came to my mind when I wrote the answer :)
AraK
+2  A: 

that would crash your program. Did you try running it? doing *object will deference a null pointer, so in fact your reference never gets assigned.

Chris H
exelent answer i understood it.
Zia ur Rahman
I don't think it'll crash. It would crash only if you attempt to access members (or methods) of object1.
Julio
It might crash, print 42 or emit blue smoke - simply derefencing a null pointer invokes undefined behaviour so you can't know in advance.
Georg Fritzsche
Just tried with GCC. I created a "NULL reference" to string without a word from the compiler. Received a segmentation fault with GDB only when trying to call a member function. IMO, while you technically "can't have" NULL references, it is important to be aware of the issues when things don't go right. - In any case, the answer is wrong. If the behavior of something is undefined, *there is no guarantee that it will crash*.
UncleBens
+4  A: 

Saying person &object1=*object is not the same thing as saying person &object1=NULL. Probably the compiler is just not smart enough to find out that you are dereferencing null pointer, but you'll get a runtime error anyway. So they are kind of true still ;)

Thomas Wanner
ok ok i understood your point very clearly thank you
Zia ur Rahman
+1  A: 

You can have a null reference, not sure why anyone would say otherwise, it is a nasty side effect of some operations. You just can't create one directly.

Charles Eli Cheese
I should not be surprised to see downvoting, I can always tell what will set off the stupid people of the planet who think they know everything yet know nothing.
Charles Eli Cheese
You can get NULL references *in practice*. Whenever one says, "you *can't* have NULL references", it should read *in a well-formed program*. Besides dereferencing NULL pointers, it might also be possible to create a reference, so that `` (the result could be anything, including NULL, I suppose). - I don't think there'd be anything wrong covering the pragmatic aspect of the problem, but your answer is vague, uninformative and argumentative ("despite what everybody says, [somewhat gibberish point here]").
UncleBens