473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

NULL pointer and null references

Hi,

I would like to know how I can return a NULL reference.
usually when I need to return an object and to know if the object
is valid I use a pointer (if the object is nto valid I return NULL) :
MyObj* MyClass::GetObj(const wxString& strObjName )
{
ReaderIt it = m_scObjs.find( strObjName );

// No Object with this name
if ( it == m_scObjs.end() ){
return NULL;
}

// Get pointer in Reader obj and set new name
MyObj* pMyobj = (MyObj*) it->second;

return pMyobj ;
}

Now Let's say I want to return a reference

MyObj& MyClass::GetObj(const wxString& strObjName )
{
ReaderIt it = m_scObjs.find( strObjName );

// No Object with this name
if ( it == m_scObjs.end() ){
return ??????????NULL;
}

// Get pointer in Reader obj and set new name
MyObj* pMyobj = (MyObj*) it->second;

return *pMyobj ;
}

Do I need to do that, I return a boolean to know if reference is valid
or is there another solution

bool MyClass::GetObj(const wxString& strObjName, MyObj& refMyobj )
{
ReaderIt it = m_scObjs.find( strObjName );

// No Object with this name
if ( it == m_scObjs.end() ){
return false;
}

// Get pointer in Reader obj and set new name
MyObj* pMyobj = (MyObj*) it->second;
refMyobj = *pMyobj;
return true ;
}

Jan 15 '06 #1
3 1674
Vincent RICHOMME wrote:
Hi,

I would like to know how I can return a NULL reference.
You can't. References by definition are an alias for an actual object.
You can, however, return a reference to a sentinel object that you
create.
usually when I need to return an object and to know if the object
is valid I use a pointer (if the object is nto valid I return NULL) :
[code snipped]
Do I need to do that, I return a boolean to know if reference is valid
or is there another solution


You should design your programs so that all your references are valid -
anything else involves undefined behavior. Either use pointers (or
better yet, some sort of smart pointer), or use a sentinel object.

You might also look at the FAQ:

http://www.parashift.com/c++-faq-lite/references.html#faq-8.6

Best regards,

Tom

Jan 15 '06 #2
On Sun, 15 Jan 2006 22:37:11 +0100, Vincent RICHOMME
<ri******@free.fr> wrote:
Hi,

I would like to know how I can return a NULL reference.
You cannot. What Thomas said...
usually when I need to return an object and to know if the object
is valid I use a pointer (if the object is nto valid I return NULL) :


[snip]

In addition to Thomas' reply, you might consider the possibility of
throwing an exception if it is an error when you cannot return a
reference to a valid object from the function. Otherwise, if it is to
be expected that the object might not exist sometimes, etc., return a
pointer instead.

Throwing an exception is sometimes the best thing to do. Like Thomas
said, though, it is really a design decision that only you can make.

--
Bob Hairgrove
No**********@Home.com
Jan 15 '06 #3
TB
Vincent RICHOMME sade:
Hi,

I would like to know how I can return a NULL reference.
usually when I need to return an object and to know if the object
is valid I use a pointer (if the object is nto valid I return NULL) :
MyObj* MyClass::GetObj(const wxString& strObjName )
{
ReaderIt it = m_scObjs.find( strObjName );

// No Object with this name
if ( it == m_scObjs.end() ){
return NULL;
}

// Get pointer in Reader obj and set new name
MyObj* pMyobj = (MyObj*) it->second;

return pMyobj ;
}

Now Let's say I want to return a reference

MyObj& MyClass::GetObj(const wxString& strObjName )
{
ReaderIt it = m_scObjs.find( strObjName );

// No Object with this name
if ( it == m_scObjs.end() ){
return ??????????NULL;
}


Is it common that this occurs or is it an exception (ie it
should never happen)? If it's the latter then throw an exception,
otherwise rethink your design.

--
TB @ SWEDEN
Jan 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Bodza Bodza | last post by:
I'm having an argument with an incumbent self-taught programmer that it is OK to use null foreign keys in database design. My take is the whole point of a foreign key is that it's not supposed...
15
by: JKop | last post by:
Does that Standard explicitly forbid the initiation of a null reference? Is there anything wrong with the following code?: void Blah( std::string const &k ) { if ( !&k ) return; // work with...
102
by: junky_fellow | last post by:
Can 0x0 be a valid virtual address in the address space of an application ? If it is valid, then the location pointed by a NULL pointer is also valid and application should not receive "SIGSEGV"...
25
by: pm940 | last post by:
Hello. I've been reading some past discussions on the NULL vs. zero. References are always made to systems or machienes that use values other than zero to represent the NULL pointer. Although...
12
by: Joe | last post by:
I might be overworked so please excuse this stupid question... Say I do the following: DataTable table = new DataTable(); myDataAdaptor.Fill(table); dataGrid1.DataSource = table;
27
by: David W | last post by:
I'm almost tearing my hair out. A colleague claimed that a null reference can exist, like this: void f( int& p ) { printf( "%d\n", p ); } int main (int argc, char *argv) {
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
76
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...
8
by: A. Anderson | last post by:
Howdy everyone, I'm experiencing a problem with a program that I'm developing. Take a look at this stack report from GDB - #0 0xb7d782a3 in strlen () from /lib/tls/i686/cmov/libc.so.6 #1 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.