Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Representing Optional Objects

I have a C++ project. I work on the project to teach myself about building a semi-realistic program in C++. It downloads content recursively from a website.

Each download has a URL for the content to download, as well as a URL for the referrer (or the URL of the page that the content was extracted from).

There is always a referrer unless it's the very first URL. I had been cheating and just treating the first URL as its own referrer. I recently changed the URL class to have a blank (or empty) representation. This feels like a hack.

Is there a way to represent optional objects in C++ without:

  • Using pointers?
  • Wasting space for the invalid object?
  • or Making a "blank" version of the object?
like image 215
Travis Parks Avatar asked Jun 07 '13 12:06

Travis Parks


1 Answers

You could use boost::optional. boost is well-respected 3rd party library; often regarded as a prototype for new stl functions: See Ralph's answer std::optional is available with new C++.

See http://www.boost.org/

like image 85
Bathsheba Avatar answered Sep 17 '22 23:09

Bathsheba