Hi Everyone,
I was wondering if there is any way to have a reference initialized
to NULL just like a pointer.
Thanks in advance!!! 9 1221
Rahul:
Hi Everyone,
I was wondering if there is any way to have a reference initialized
to NULL just like a pointer.
Thanks in advance!!!
There is of course:
int *p = 0;
int &i = *p;
Only problem with this though is that the Standard leaves the behaviour
as explicitly undefined when you dereference a null pointer. So that
means don't do it in a portable program unless you want the hard disk
heads to crash and destroy all data on your disk :-D
And as far as I know, there's no other means of getting a "reference to
null" than dereferencing a null pointer (...I can't think of any off-hand
anyway).
Maybe your own system will let you do it, but it's not a habit you wanna
pick up if you want to program portably.
--
Tomás Ó hÉilidhe
On 2007-12-01 07:34:51 -0500, Rahul <sa*****@yahoo.co.insaid:
Hi Everyone,
I was wondering if there is any way to have a reference initialized
to NULL just like a pointer.
I suppose you can try
Object &x = *(Object *)0;
And then you can test for NULL reference with
if (&x == 0) ...
But just because you could, should you?
--
-kira
On Dec 1, 5:58 pm, Kira Yamato <kira...@earthlink.netwrote:
On 2007-12-01 07:34:51 -0500, Rahul <sam_...@yahoo.co.insaid:
Hi Everyone,
I was wondering if there is any way to have a reference initialized
to NULL just like a pointer.
I suppose you can try
Object &x = *(Object *)0;
And then you can test for NULL reference with
if (&x == 0) ...
But just because you could, should you?
--
-kira
I just wanted to know the possibility of passing a NULL reference to a
copy constructor and as per your code i'm able to do so and vc++ is
crashing :-)
class copu
{
int j;
public:
copu(const copu& obj)
{
printf("in copy constructor...%d\n",j);
j = obj.j; //-crash over
here ;-)
printf("in copy constructor...2. %d\n",j);
}
copu()
{
j = 10;
printf("in default constructor...%d\n",j);
}
};
int main()
{
copu obj;
copu& ref = *(copu*)0;
copu sam = ref; //-invokes the copy constructor
}
On Dec 1, 6:19 pm, Rahul <sam_...@yahoo.co.inwrote:
On Dec 1, 5:58 pm, Kira Yamato <kira...@earthlink.netwrote:
On 2007-12-01 07:34:51 -0500, Rahul <sam_...@yahoo.co.insaid:
Hi Everyone,
I was wondering if there is any way to have a reference initialized
to NULL just like a pointer.
I suppose you can try
Object &x = *(Object *)0;
And then you can test for NULL reference with
if (&x == 0) ...
But just because you could, should you?
--
-kira
I just wanted to know the possibility of passing a NULL reference to a
copy constructor and as per your code i'm able to do so and vc++ is
crashing :-)
class copu
{
int j;
public:
copu(const copu& obj)
{
printf("in copy constructor...%d\n",j);
j = obj.j; //-crash over
here ;-)
printf("in copy constructor...2. %d\n",j);
}
copu()
{
j = 10;
printf("in default constructor...%d\n",j);
}
};
int main()
{
copu obj;
copu& ref = *(copu*)0;
copu sam = ref; //-invokes the copy constructor
}
So is there anyway to avoid referring to a variable of a NULL
reference? A developer of a class should consider this for a robust
class, he can't expect the user of the class to do the correct things.
I just want to have a graceful exit from the copy constructor...
In my opinion, you may check it like this:
copu::copu(const copu& obj)
{
assert(&obj != NULL);
//...
j = obj.j
//...
}
But since the obj is invalid, the copy constructing can not be all
right. Aborting by assert is the best. :P
On 1 Dec., 14:22, Rahul <sam_...@yahoo.co.inwrote:
On Dec 1, 6:19 pm, Rahul <sam_...@yahoo.co.inwrote:
On Dec 1, 5:58 pm, Kira Yamato <kira...@earthlink.netwrote:
On 2007-12-01 07:34:51 -0500, Rahul <sam_...@yahoo.co.insaid:
Hi Everyone,
I was wondering if there is any way to have a reference initialized
to NULL just like a pointer.
I suppose you can try
Object &x = *(Object *)0;
And then you can test for NULL reference with
if (&x == 0) ...
But just because you could, should you?
--
-kira
I just wanted to know the possibility of passing a NULL reference to a
copy constructor and as per your code i'm able to do so and vc++ is
crashing :-)
class copu
{
int j;
public:
copu(const copu& obj)
{
printf("in copy constructor...%d\n",j);
j = obj.j; //-crash over
here ;-)
printf("in copy constructor...2. %d\n",j);
}
copu()
{
j = 10;
printf("in default constructor...%d\n",j);
}
};
int main()
{
copu obj;
copu& ref = *(copu*)0;
copu sam = ref; //-invokes the copy constructor
}
So is there anyway to avoid referring to a variable of a NULL
reference? A developer of a class should consider this for a robust
class, he can't expect the user of the class to do the correct things.
I just want to have a graceful exit from the copy constructor...
The problem with the code above is that you invoke undefined behaviour
by dereferencing a null pointer. This code is not worth bothering
about (the program becomes invalid at that point), so there is no
reason and no need to check for this.
/Peter
Rahul wrote:
So is there anyway to avoid referring to a variable of a NULL
reference?
Yes. Don't initialize a reference by dereferencing a null pointer. The place
where the error happens (and where the C++ standard says that the behavior
becomes undefined) is the place where you do that, not the place where you
use the invalid reference.
A developer of a class should consider this for a robust class, he can't
expect the user of the class to do the correct things.
He must. There is always a way to screw things up. It is more likely that
the user provides a reference to an object that is already destroyed. That
would be just as disastrous, and there is no way at all to check for that.
I just want to have a graceful exit from the copy constructor...
By doing what?
Rahul wrote:
Hi Everyone,
I was wondering if there is any way to have a reference initialized
to NULL just like a pointer.
Thanks in advance!!!
No, there's no such thing as a null reference and anything
that coerces one almost certainly causes undefined behavior.
One thing you can do is use a reference to an object that you
can tell from a valid one:
class C {
public:
static C my_null;
bool IsNull(const C& t) {
return &t == &my_null;
}
void SomeFunc(const C& c = my_null) {
if(IsNull(c)) {...
On Dec 1, 1:34 pm, Rahul <sam_...@yahoo.co.inwrote:
I was wondering if there is any way to have a reference initialized
to NULL just like a pointer.
Not legally, unless your compiler supports it as an extension.
(I don't know of any that do.) It's undefined behavior;
anything can happen.
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Web Developer |
last post by:
Hi,
Can someone provide a short and concise statement(s) on the difference
between pointers and references. A graphical representation (via links?) of
both would be much appreciated as well.
...
|
by: Ed Smith |
last post by:
I have two questions about REFERENCES:
1. It appears that mySQL treats REFERENCES associated with an
attribute differently than FOREIGN KEY (<blah>) REFERENCES...
Specifically, the first form...
|
by: JKop |
last post by:
I understand variables/objects and pointer variables perfectly:
int X = 5;
int* pX = &X;
*pX = 4;
int** ppX = &pX:
|
by: john townsley |
last post by:
are there any differences when using pointers or passing by reference when
using
1) basic variables
2)complex variable types like arrays,structures
it seems easier to simply pass by reference...
|
by: Robert Zurer |
last post by:
I have a large pool of business objects all referencing one another in
various ways.
In the client application I want to do something like
employee.Delete();
Behind the scenes, I want to...
| |
by: codebloatation |
last post by:
I know how to use references but i DO not get WHY they exist other than
to add to the language. Are they actually needed for anything?
|
by: Scott Goodwin |
last post by:
The following SQL:
create table toinherit (
id integer primary key
);
create table leftside (
leftname varchar(64) not null unique
) inherits (toinherit);
|
by: valentin tihomirov |
last post by:
As explained in "Using pointers vs. references"
http://groups.google.ee/group/borland.public.delphi.objectpascal/browse_thread/thread/683c30f161fc1e9c/ab294c7b02e8faca#ab294c7b02e8faca ,
the...
|
by: sip.address |
last post by:
Hi everyone,
A while ago I asked for help about using smart pointers (ie.
shared_ptr) in cyclic situations. Weak ptrs popped out and I gave it a
try, but to be honest, I don't feel very...
|
by: Simon Saize |
last post by:
Hi -
What's the point of having references (&)? Why don't we just use
pointers (*)?
Thanks.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |