469,913 Members | 2,475 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,913 developers. It's quick & easy.

why reference cannot represent NULL object?

Hello all,

Why reference cannot be able to represent a NULL object?

Best regards,
Robert

Aug 19 '05 #1
8 2258
Robert schreef:
Hello all,

Why reference cannot be able to represent a NULL object?


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

Aug 19 '05 #2

Robert wrote:
Hello all,

Why reference cannot be able to represent a NULL object?

Best regards,
Robert


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

Aug 19 '05 #3
> Hello all,

Why reference cannot be able to represent a NULL object?

Best regards,
Robert


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
Aug 19 '05 #4
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

Aug 19 '05 #5
On Fri, 19 Aug 2005 15:16:31 +0200, Steffen <s.*********@gmx.de>
wrote:
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


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
No**********@Home.com
Aug 19 '05 #6
Steffen wrote:
Hi,

I have a somewhat related question:
is it defined what happens to a reference after the object it references
is destroyed?


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.
Aug 20 '05 #7
Well, I personally consider C++'s "undefined behavior" less bizzare then the
COM's "must succeed barring catastrophic failure..."

Ben
Aug 20 '05 #8
red floyd wrote:
is it defined what happens to a reference after the object it
references is destroyed?


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

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

Steffen

Aug 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by JKop | last post: by
7 posts views Thread by Wolfgang Kreuzer | last post: by
10 posts views Thread by Robert | last post: by
7 posts views Thread by Xiaoshen Li | last post: by
1 post views Thread by vishnu | last post: by
68 posts views Thread by Jim Langston | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.