C ++ Representation of Additional Objects

I have a C ++ project. I am working on a project to teach myself how to create a semi-realistic C ++ program. It recursively downloads content from a website.

Each download has a URL for the downloaded content, as well as a referrer URL (or the URL of the page on which the content was extracted).

There is always a referrer if it is not the very first URL. I cheated and just looked at the first URL as my own referrer. I recently changed the URL class to have an empty (or empty) representation. It looks like a hack.

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

  • Using pointers?
  • Drop space for invalid object?
  • or creating an "empty" version of an object?
+6
source share
2 answers

You can use boost :: optional. promotion - a respected third-party library; often seen as a prototype for new stl functions: See Ralph answer std :: optional available with new C ++.

See http://www.boost.org/

+5
source

Use the std::optional template if you have a C ++ 14 compiler. If not, you can use boost::optional or std::unique_ptr . You cannot avoid using pointers and waste space on an invalid object. std::optional will contain an optional std::unique_ptr obviously will point to this.

+5
source

Source: https://habr.com/ru/post/946778/


All Articles