Solved

c++ Nullable implementation

Posted on 2009-07-08
3
1,724 Views
Last Modified: 2012-06-27
Do you know a good c++ implementation of the Nullable Generic in c#?
Something like Nullable<class T>

Is there a good way to avoid using the .Value member to get the value?
Nullable<float> Number;
 
float x=Number.Value; //Can I avoid the '.Value' here somehow? and use x=Number? without custom casting staff?

Open in new window

0
Comment
Question by:Hanochr
3 Comments
 
LVL 40

Accepted Solution

by:
evilrix earned 250 total points
ID: 24802668
Not being a C# expert and going just by the docs I think something like below (with a few tweaks maybe) will model the behavior you are after.

>> Is there a good way to avoid using the .Value member to get the value?
I've overloaded operator *()

template < typename T>
class Nullable
{
public:
   Nullable(T * p = 0)
      :p_(p)
   {
   }
 
   bool HadValue() const
   {
      return p_ != 0;
   }
 
   T const & Value() const
   {
      return *p_;
   }
 
   T & Value()
   {
      return *p_;
   }
 
   T const & operator * () const
   {
      return *p_;
   }
 
   T & operator * ()
   {
      return *p_;
   }
 
   T * p_;
 
private:
};
 
int main()
{
   bool b = true;
 
   Nullable<bool> bnYes(&b);
   Nullable<bool> bnNo;
}

Open in new window

0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 250 total points
ID: 24802720
I see evilrix already posted his suggestion. Mine's slightly different, so I'll post it too (it's a rough example, just to give an idea).


>> Is there a good way to avoid using the .Value member to get the value?

An overloaded cast operator like below should do the trick.
#include <iostream>
 
template <class T>
class Nullable {
  private :
    T data;
    bool null;
 
  public :
    Nullable(T d) : data(d), null(false) { }
    Nullable() : data(), null(true) { }
 
    bool hasValue() { return !null; }
    T value() { return data; }
    
    operator T() { return data; }
};
 
int main(void) {
  Nullable<float> numberA = Nullable<float>(5.0);
  Nullable<float> numberB = Nullable<float>();
  
  (numberA.hasValue()) ? (std::cout << numberA.value()) : (std::cout << "NULL"); std::cout << std::endl;
  (numberB.hasValue()) ? (std::cout << numberB.value()) : (std::cout << "NULL"); std::cout << std::endl;
  
  float value = numberA;
  std::cout << value << std::endl;
  
  return 0;
}

Open in new window

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now