2

In C++, how to deal with NULL values in a context where we might expect a string or an integer?

Typically, the context would be when retrieving data from a database, e.g. with a LEFT JOIN where some non-existing values in some column would be represented by a NuLL value instead of the expected string or integer.

Strong casting in C++ makes this type of scenario much more difficult to deal with than with PHP.

In PHP, we can easily have a if/else switch using the === operator:

if ($value === NULL) {
  // No data.
} else {
  // We have some valid data.
}

What would the equivalent look like in C++?

I've searched and couldn't find a C++ relevant question.

The question is valid in general. In my particular case, I am using the dbixx SQL library.

BobbyShaftoe
  • 28,337
  • 7
  • 52
  • 74
augustin
  • 14,373
  • 13
  • 66
  • 79
  • How does the library represent NULL values in the first place? – Ignacio Vazquez-Abrams Nov 05 '10 at 02:04
  • 1
    Huh? 1) This isn't PHP. It doesn't matter what you can "easily do" in PHP, because, believe it or not, C++ isn't PHP. So why even mention it? "What would the equivalent look like in C++?" Who says there is necessarily any equivalence? They're likely different languages for a reason... 2) Do you have an actual situation you're trying to make work? Then post that. Don't ask a theoretical question, especially where it doesn't make sense language-wise (C++ variables don't have a generic "null" state, they always hold a value or are uninitialized). – GManNickG Nov 05 '10 at 02:04
  • 3) Pertaining to number two, if you posted your real code we could use the documentation of your library to try and solve your problem. But right now, it just doesn't make sense. So: What's your real situation? What actual code are you working with, what's the big picture? – GManNickG Nov 05 '10 at 02:05
  • Related: [Nullable values in C++](http://stackoverflow.com/q/2537942/237483) – Christian Ammer Mar 05 '13 at 08:34

4 Answers4

4

boost::optional is a natural way to represent maybe types in c++ with strong type checking (types that could be null must be checked for null)

SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
  • Thanks. I'm reading through the boost::optional documentation and I may end up using it. Thanks a lot. – augustin Nov 05 '10 at 02:52
3

From http://art-blog.no-ip.info/wikipp/en/page/ref_dbixx_row :

Function cols() returns number of columns in the row. isnull() returns if specific filed is null, using it's column number (starting from 1) or column name;

bool operator[] is just syntactic sugar for isnull().

In your question you stated you were using dbixx, so I have linked to their documentation. This will always be specific to the library you are using.

BobbyShaftoe
  • 28,337
  • 7
  • 52
  • 74
  • Thanks. Believe me, I had checked the documentation, more than once. However, since I am still a beginner, it was not immediately apparent how I could use what you quote. I may need to refactor my code to make use of this. My question was more generic: how you you, experienced C++ developers, deal with similar situations. As you say, it's specific to the library (which itself is a different way of thinking than in PHP). – augustin Nov 05 '10 at 02:37
  • Even though I had seen that in the dbixx documentation, I am still glad I asked because the solution to my specific problem might be solved by the use of boost::optional as suggested by TokenMacGuy. – augustin Nov 05 '10 at 02:55
1

For string pointers (and object pointers in general), it's easy, they can hold 0 to show that they're null.

For integers, you have to rely on an external variable that says if the object returned is null or not. Or if you pass it as a pointer, you can use a 0 pointer like above.

Blindy
  • 65,249
  • 10
  • 91
  • 131
  • Your language is confusing. I think you mean "For string pointers (and object pointers in general) ..." – davmac Nov 05 '10 at 02:52
  • Yea, I meant pointers for both. Too used to C# now, forgot you could allocate them on the stack. – Blindy Nov 05 '10 at 04:46
1

It may sound like a general concept, but there is no general answer for C++. The language itself does not have a truly general "NULL" value that can by used with all types to distinguish from having been set to an actual value.

So the answer in any situation entirely depends on the library or whatever that you're working with and how it has been designed to indicate "NULL" values. The return values of functions used to access the data may perhaps indicate whether the data is NULL or otherwise. Or there may be something like a separate IsNull() function. Or the library may use some other different scheme. There are many possibilities.

The only possible general answer is: read the documentation of the library to find out how they specifically deal with it.

TheUndeadFish
  • 8,058
  • 1
  • 23
  • 17
  • Thanks. I am a beginner with C++ and I am still struggling with the basics. Things that I used to do naturally in PHP are no longer applicable in the C++ world. I have to learn to think in a different way about some key problems. – augustin Nov 05 '10 at 02:34
  • @augustin: You should get an [introductory level book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), then. You aren't going to learn any other way. And you're correct: forget you know any other languages. Just because you can drive a car doesn't mean you can fly a plane, so what use is there thinking about driving a car while you're trying to be a pilot? – GManNickG Nov 05 '10 at 02:40
  • @GMan, thanks. I mentioned PHP because it's what I know and it had the merit of perfectly explaining my dilemma. – augustin Nov 05 '10 at 02:47