|
-
May 6th, 2009, 03:43 PM
#5
Re: NULL Values
Stroustrup suggested to use 0 for pointers, indicating NULL was a confused issue, a vestige from C.
BleaS: NULL, if used at all, applies to pointers, usually. It hardly ever applies to numeric values, though because it's a define, it can.
What you're talking about is a 'non' grade value, not a null per se.
The point regarding a status flag is a fair one, in other typical situations I've seen -1 used to indicate that a value which is otherwise always a positive quantity, including zero, is otherwise uninitialized.
Beyond that, maximum quantities, like the maximum extent of an integer, can be used.
Back to the flag concept, consider a class for this concept. Create a class that can operate as the numeric type you want (you can even use templates for this, so the concept is adaptable). The class would have operators for all the math/comparison/construction attributes that let it function just as the POD type it stands in for, but also includes a bool as a status flag. Assuming 'false' means uninitialized, the default constructor sets this bool to false.
Any operation that 'initializes' this value (operator = for example) can also set this flag to true, so you don't even have to cause application code to manage the flag. "Get" functions can tell you if the value is initialized or not.
Code:
class V
{
private:
int value;
bool initialized;
public:
V() : value( 0 ), intialized( false ) {}
bool IsNull() { return !initialized; }
.....operators, etc....
};
This is more useful if you have lots of values, and would otherwise have lots of value/bool pairs to manage.
If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|