Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6651497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:01:11+00:00 2026-05-26T01:01:11+00:00

Possible Duplicate: Nullable values in C++ What is the best way to represent nullable

  • 0

Possible Duplicate:
Nullable values in C++

What is the best way to represent nullable member in C++?

In C#, we can use Nullable<T> type. Such a data type is very much needed as not everything can have meaningful value. It is so important data type that @Jon Skeet has spent one entire chapter, spanned over 27 pages, describing only Nullable<T> in his outstanding book C# in Depth.

One simple example can be a Person class1, defined as:

struct Person
{
  std::string Name;
  DateTime    Birth;
  DateTime    Death;
  //...
};

As a person always have birthdate, so the Birth member of the above class will always have some meaningful value. But how about Death? What should it value be if the person is alive? In C#, this member can be declared as Nullable<DataTime>2 which can be assigned with null if the person is alive.

In C++, what is the best way to solve this? As of now, I’ve only one solution in mind: declare the member as pointer:

 DataTime *Death;

Now its value can be nullptr when the person is alive. But it forces the use of new for dead person, as it’s going to have some valid value. It in turn implies one cannot rely on the default copy-semantic code generated by the compiler. The programmer has to write copy-constructor, copy-assignment, destructor following rule of three (C++03), Or in C++11, rule of five.

So do we have any better, elegant solution to this problem than just making it pointer?


1. Other examples include relational database tables, as in many DBMSs columns can be nullable.

2. There is also a shorthand for this. One can write DataTime? which is exactly same as Nullable<DateTime>.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:01:12+00:00Added an answer on May 26, 2026 at 1:01 am

    You could look into Boost.Optional:

    struct Person
    {
      std::string               Name;
      DateTime                  Birth;
      boost::optional<DateTime> Death;
      //...
    };
    
    • Your Death is “uninitialised” at first.
    • You can then assign a value to it with =, like Death = myDateTime.
    • When Death.is_initialized(), you can use Death.get().
    • Uninitialise it again with Death.reset().

    For simple cases like this, though, it’s usually considered more coherent to just pick your own blatant sentinel value like, say, a DateTime of “0000-00-00 00:00:00”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Possible Duplicate: How to check if an object is nullable? I have a System.Type
Possible Duplicate: Best way to really grok Java for a C# guy I worked
Possible Duplicate: Best way to determine if two path reference to same file in
Possible Duplicate: How to: URL re-writing in PHP? How can a website use an
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: Nullable types and the ternary operator. Why won’t this work? This is
Possible Duplicate: Nullable types and the ternary operator. Why won’t this work? for example:
Possible Duplicate: How can I take screenshots with Perl? How can I take a
Possible Duplicate: How to horizontally center a div? One simple way to make an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.