why reference cannot represent NULL object?

R

Robert

Hello all,

Why reference cannot be able to represent a NULL object?

Best regards,
Robert
 
M

msalters

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
 
E

Earl Purple

Robert said:
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
 
B

benben

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
 
S

Steffen

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
 
B

Bob Hairgrove

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

benben

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

Ben
 
S

Steffen

red said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top