The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

C++ pattern for nullable object?

I am currently convert a large Java project to C++. In Java, you can assign null to any object instance, and it is handy when used as a condition, such as the code below:

ObjectA GetObjectA() { ... }

if (GetObjectA()!=null ) { ...}

The problem is that in C++ nullable object and reference are not allowed. I have to use something like:

std::pair<bool, ObjectA> GetObjectA() { }
std::pair<bool, ObjectA> a = GetObjectA();
if ( a.first ) { }

This is too tedious. Is there a good template pattern emulating the nullable object behavior?
Friday, December 22, 2006
How about using a Smart pointer, like Boost shared pointer. You can then test if something is assigned to it or not.
Neville Franks Send private email
Friday, December 22, 2006
Use a pointer, not a reference: there is no problem assigning NULL to a pointer.
Jeff Dutky Send private email
Friday, December 22, 2006
bool ObjectA::IsInitialized() const {...}


bool ObjectA::IsNull() const {...}
Friday, December 22, 2006
Java object instance corresponds roughly to the pointer to the object in C++. Except of course C++ has no GC. Smart (optionally reference counted) pointers help to releave the burden of manual deallocation.

Btw, the direct assignment of objects incurs approximately the same performance penalty as cloning in Java. However, stack-allocated objects (local variables) are handy when their lifetime is naturally bounded by a scope (of a function).
Saturday, December 23, 2006
May I ask why you are migrating from Java to C++?  The mantra on the securecoding mailing list these days seems to be use anything but C++.

I'm currently sitting in that dilemna right now for a new project where C++ and Java are the primary possibilities.
Saturday, December 23, 2006
Yin-So Chen Send private email
Sunday, December 24, 2006
I second the chap who suggested the notion of using a pointer. There is nothing in C++ that maps simply to the Java behaviour of the = operator, but when it comes to objects pointers are about as good as you'll get.

Good luck... it sounds like you will need it.
Monday, December 25, 2006
Another approach is to have a global static object that you treat as "null".

ObjectA &GetObjectA() {
  if (mInitialized) {
    return mObjA;
  else {
    return gNullObj;

if (GetObjectA()!=gNullObj) { ... }

You can go further by making a NullObjectA class that derives from ObjectA but throws exceptions whenever you try to call any of its methods (and then make gNullObj a NullObjectA).
You can also make an operator bool() or the class that always returns true for ObjectA and false for NullObjectA so you can still do
if (GetObjectA()) { ... }
(Note that there are caveats here, Google "safe bool idiom".)
Monday, January 01, 2007

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz