Connecting Tech Pros Worldwide Forums | Help | Site Map

why reference cannot represent NULL object?

Robert
Guest
 
Posts: n/a
#1: Aug 19 '05
Hello all,

Why reference cannot be able to represent a NULL object?

Best regards,
Robert


msalters
Guest
 
Posts: n/a
#2: Aug 19 '05

re: why reference cannot represent NULL object?


Robert schreef:
[color=blue]
> Hello all,
>
> Why reference cannot be able to represent a NULL object?[/color]

Because there are no NULL objects. A reference refers to an object.
Same reason why 'this' cannot be 0; this points to the object on
which a member function was called.

HTH,
Michiel Salters

Earl Purple
Guest
 
Posts: n/a
#3: Aug 19 '05

re: why reference cannot represent NULL object?



Robert wrote:[color=blue]
> Hello all,
>
> Why reference cannot be able to represent a NULL object?
>
> Best regards,
> Robert[/color]

What do you mean by a NULL object? If you mean whatever happens to
exist at *0 then that is meaningless.

You can devise your own classes to have possible NULL-state.

class MyClass
{
private:
bool nullFlag;
// other stuff
public:
MyClass() : nullFlag( true ) {}
bool isNull() const { return nullFlag; }
// other stuff
void init(); // will initialise and remove null-state
};

MyClass aMyClass;
MyClass & refToANullMyClass = aMyClass;
refToANullMyClass.isNull(); // will return true

benben
Guest
 
Posts: n/a
#4: Aug 19 '05

re: why reference cannot represent NULL object?


> Hello all,[color=blue]
>
> Why reference cannot be able to represent a NULL object?
>
> Best regards,
> Robert
>[/color]

If your "NULL object" is defined as follows:

const int NULL = 0;

then you CAN have a reference representing a NULL object:

const int& null_ref = NULL;

Note that null_ref is just another name of NULL, which is different from
what a pointer is, however.

Ben


Steffen
Guest
 
Posts: n/a
#5: Aug 19 '05

re: why reference cannot represent NULL object?


Hi,

I have a somewhat related question:
is it defined what happens to a reference after the object it references
is destroyed? It looks like there would be some sort of copy:


#include <iostream>
using namespace std;

class T {
public:
T(int i) {n=i;};
int n;
void speak(void) {cout << "Hello " << n << endl;};
};

class S{
public:
S(void) {};
void shout(void) {cout << "HEEYY!" << endl;};
};

int main(void)
{
T *t_ptr = new T(7);
T &t_ref = *t_ptr;

cout << "t_ptr: " << t_ptr << '\n'
<< "&t_ref: " << &t_ref << '\n';

t_ref.speak();

delete t_ptr;

t_ref.speak();

S *s_ptr = new S;

cout << "s_ptr: " << s_ptr << '\n'
<< "&t_ref: " << &t_ref << endl;

s_ptr->shout();
t_ref.speak();

delete s_ptr;
return 0;
}


produces:

t_ptr: 0x8049f40
&t_ref: 0x8049f40
Hello 7
Hello 0
s_ptr: 0x8049f40
&t_ref: 0x8049f40
HEEYY!
Hello 0


But why is the address the same?

Steffen

Bob Hairgrove
Guest
 
Posts: n/a
#6: Aug 19 '05

re: why reference cannot represent NULL object?


On Fri, 19 Aug 2005 15:16:31 +0200, Steffen <s.weinstock@gmx.de>
wrote:
[color=blue]
>Hi,
>
>I have a somewhat related question:
>is it defined what happens to a reference after the object it references
>is destroyed? It looks like there would be some sort of copy:
>
>
>#include <iostream>
>using namespace std;
>
>class T {
>public:
> T(int i) {n=i;};
> int n;
> void speak(void) {cout << "Hello " << n << endl;};
>};
>
>class S{
>public:
> S(void) {};
> void shout(void) {cout << "HEEYY!" << endl;};
>};
>
>int main(void)
>{
> T *t_ptr = new T(7);
> T &t_ref = *t_ptr;
>
> cout << "t_ptr: " << t_ptr << '\n'
> << "&t_ref: " << &t_ref << '\n';
>
> t_ref.speak();
>
> delete t_ptr;
>
> t_ref.speak();
>
> S *s_ptr = new S;
>
> cout << "s_ptr: " << s_ptr << '\n'
> << "&t_ref: " << &t_ref << endl;
>
> s_ptr->shout();
> t_ref.speak();
>
> delete s_ptr;
> return 0;
>}
>
>
>produces:
>
>t_ptr: 0x8049f40
>&t_ref: 0x8049f40
>Hello 7
>Hello 0
>s_ptr: 0x8049f40
>&t_ref: 0x8049f40
>HEEYY!
>Hello 0
>
>
>But why is the address the same?
>
>Steffen[/color]

It's undefined behavior. Section 8.3.2, paragraph 4 of the C++
standard says so.

If it works at all one time by pure luck, it might not work the next,
and most certainly will not work all the time on some operating
systems and/or compilers.

--
Bob Hairgrove
NoSpamPlease@Home.com
red floyd
Guest
 
Posts: n/a
#7: Aug 20 '05

re: why reference cannot represent NULL object?


Steffen wrote:[color=blue]
> Hi,
>
> I have a somewhat related question:
> is it defined what happens to a reference after the object it references
> is destroyed?[/color]

With apologies to _Ghostbusters_, this is some of the things that
*could* happen:

* Fire and brimstone coming down from the skies. Rivers and seas boiling.
* Forty years of darkness. Earthquakes, volcanoes...
* The dead rising from the grave.
* Human sacrifice, dogs and cats living together - mass hysteria

In other words, pretty much anything, since it's UB.


benben
Guest
 
Posts: n/a
#8: Aug 20 '05

re: why reference cannot represent NULL object?


Well, I personally consider C++'s "undefined behavior" less bizzare then the
COM's "must succeed barring catastrophic failure..."

Ben


Steffen
Guest
 
Posts: n/a
#9: Aug 22 '05

re: why reference cannot represent NULL object?


red floyd wrote:[color=blue][color=green]
>> is it defined what happens to a reference after the object it
>> references is destroyed?[/color]
>
> With apologies to _Ghostbusters_, this is some of the things that
> *could* happen:
>
> * Fire and brimstone coming down from the skies. Rivers and seas boiling.
> * Forty years of darkness. Earthquakes, volcanoes...
> * The dead rising from the grave.
> * Human sacrifice, dogs and cats living together - mass hysteria[/color]


.... Well, I know that references are powerful things, but I guess that
on most machines there are no reliable drivers for THAT ...

Steffen

Closed Thread