Connecting Tech Pros Worldwide Help | Site Map

why reference cannot represent NULL object?

  #1  
Old August 19th, 2005, 01:25 PM
Robert
Guest
 
Posts: n/a
Hello all,

Why reference cannot be able to represent a NULL object?

Best regards,
Robert

  #2  
Old August 19th, 2005, 01:35 PM
msalters
Guest
 
Posts: n/a

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

  #3  
Old August 19th, 2005, 01:55 PM
Earl Purple
Guest
 
Posts: n/a

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

  #4  
Old August 19th, 2005, 02:05 PM
benben
Guest
 
Posts: n/a

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


  #5  
Old August 19th, 2005, 02:25 PM
Steffen
Guest
 
Posts: n/a

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

  #6  
Old August 19th, 2005, 02:35 PM
Bob Hairgrove
Guest
 
Posts: n/a

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
  #7  
Old August 20th, 2005, 01:15 AM
red floyd
Guest
 
Posts: n/a

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.


  #8  
Old August 20th, 2005, 07:15 AM
benben
Guest
 
Posts: n/a

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


  #9  
Old August 22nd, 2005, 09:35 AM
Steffen
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
why pass-by-reference of a pointer variable? Xiaoshen Li answers 7 December 30th, 2005 09:35 PM
Protected static members, abstract classes, object composition vs. subclassing Kevin Prichard answers 11 November 29th, 2005 01:45 AM
Pass by Pointer * or Pass by Reference &? Robert answers 10 August 24th, 2005 06:05 PM
Pass-by-reference instead of pass-by-pointer = a bad idea? Mr A answers 110 July 23rd, 2005 07:17 AM