473,804 Members | 2,989 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1708
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.c om/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**********@Ho me.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
42428
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 to be optional, it's very definition is it's a necessary link to the parent table and part of the definition. If it's optional it shouldn't be part of the definition of a table and should be in a linking table instead. Comments?
15
2248
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 k }
102
6076
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" ( i am talking of unix machine ) while trying to read that location. Then how can i distinguish between a NULL pointer and an invalid location ? Is this essential that NULL pointer should not point to any of the location in the virtual address...
25
4446
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 not a practice I follow, there are no doubt millions of lines of open source libraries and applications that do things like: p = malloc(...);
12
16773
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
4240
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
3271
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 my object is destroyed, I want my object to un-register this event. If I don't then the object would never be destroyed until the object I passed in the constructor is destroyed. I have implemented a Dispose(), Dispose(bool), and ~Finalize...
76
4729
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 pointers are allowed to be null, while references must refer an existing variable of required type. The null is normally used for making optional parameters. But there is no way to pass null reference in C#. Something is missing.
8
2305
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 0xb7d4c2f7 in vfprintf () from /lib/tls/i686/cmov/libc.so.6 #2 0xb7d6441b in vsprintf () from /lib/tls/i686/cmov/libc.so.6 #3 0x08049ba0 in character_data::printf (this=0x800, argument=0x0) at character.c:198
0
9572
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10303
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10070
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7608
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6845
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5508
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.