-2

I am studying a bit of code, which contains a factory method, if I am remembering my object orientation correctly.

The factory method and the related classes can be described by the following pseudo-C++.

The class Actor is the base class for the various implementations of concrete actions or operations, which in turn are implemented as derived classes.

The factory method createActor receives arguments which are read from an input script, hence prior to calling a constructor some error checking is being done.

I noticed, that in all cases when an error is detected, I find a return 0 statement. This can obviously be done, since the code (not written by me) compiles and runs.

However, the factory method is supposed to return a pointer to an Actor-class. Is in this case return 0 simply an obscure way to return NULL? Maybe I am overthinking this.

class Actor
{
  class ActorVariantA : Actor
  {
  }
  // all other ActorVariants are omitted for brevity

  Actor* createActor(arguments)
  {
    if (errorCondition)
      return 0
    if (conditionA)
      return new ActorVariantA(arguments)
  }
}

I found a somewhat related question, which in turn led me to Stroustrup himself.

So, the answer to my question would be: sort-of, but don't do it.

Dohn Joe
  • 313
  • 4
  • 15
  • 2
    Instead of `return 0` you should `return nullptr;` Also since 2011 / c++11 standard `NULL` should be avoided and `nullptr` should be used instead. – drescherjm Nov 10 '22 at 13:37
  • As you said, it's pseudo-C++, so why do you worry about 0 meaning? – 273K Nov 10 '22 at 13:38
  • I am asking about actual C++ code, however, I did not want to copy&paste the actual code, I re-wrote the specific bits I am curious about into my pseudo-C++. – Dohn Joe Nov 10 '22 at 13:40
  • In real code use `nullptr` like I said in the first comment. – drescherjm Nov 10 '22 at 13:41
  • For a function that returns a pointer, `return 0`, `return NULL` (if a standard header is `#include`d that `#define`s `NULL`), and (C++11 and later) `return nullptr` all cause the function to return a null pointer (a pointer equal to the null pointer constant). In each case, the caller can test if the returned pointer is null, and will have undefined behaviour if a null pointer is dereferenced. If using C++11 and later, `return nullptr` is preferred over the other options since it offers improved safety and less ambiguity in some situations, and less chance of being nagged by other developers. – Peter Nov 10 '22 at 13:50
  • In my experience, it's better for a factory function to `throw` an exception if it fails rather than return a `nullptr` (...that being said, use your discretion). Also, the factory function should be `static`, and instead of returning a raw pointer I recommend `std::unique_ptr` as the return type. – Eljay Nov 10 '22 at 13:51
  • Please do your search to related questions **before** you post your own one. – Jakob Stark Nov 10 '22 at 13:52
  • 1
    You could throw an exception instead. – drescherjm Nov 10 '22 at 13:54
  • I am totally confused as to what remains as question after reading https://www.stroustrup.com/bs_faq2.html#null !?! Please do not edit answers into the question. If you want to write an answer you can write an answer – 463035818_is_not_an_ai Nov 10 '22 at 14:07
  • I would also just throw an exception (unless I have hard realtime requirements), it is the least surprising. And ensures you dont have to document a special return cases. – Pepijn Kramer Nov 10 '22 at 16:55
  • Interestingly, error handling is done by the caller of the factory method like this: `if(actor_ == 0) errorMessage("Error message");` With `actor_` being of the type `Actor*`. I guess older versions/standards of C++ did not clearly and/or strictly distinguish between a 0-value, a NULL-value and a null-pointer. – Dohn Joe Nov 10 '22 at 17:25
  • @DohnJoe there is a clear distinction between `0` and `nullptr`. They are off different type, though both can be converted to an `Actor*` – 463035818_is_not_an_ai Nov 11 '22 at 08:36
  • I guess that's a hold-over from the low-level, when each type, i.e., 0, Null and nullptr, is an all-bits-are-0 type; then one can quite happily interchange them. However, as a casual programmer, I would find more peace of mind, if different things were kept separate, regardless of having the same machine representation. I guess, that's were my confusion came from. The method defines a pointer to a class as return type, yet returns a 0 integer. Valid on a low level, yet somewhat confusing. – Dohn Joe Nov 11 '22 at 09:09

1 Answers1

3

yes, you are overthinking this. It should really be return nullptr, but in this case return 0 is equivalent.

bolov
  • 72,283
  • 15
  • 145
  • 224