Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


c++ Nullable implementation

Posted on 2009-07-08
Medium Priority
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

Question by:Hanochr
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 40

Accepted Solution

evilrix earned 1000 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
   Nullable(T * p = 0)
   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_;
int main()
   bool b = true;
   Nullable<bool> bnYes(&b);
   Nullable<bool> bnNo;

Open in new window

LVL 53

Assisted Solution

Infinity08 earned 1000 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


Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

609 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