2 ms·
It has been many years since I used C++ But returning null is an obvious choice. Combined types which return the structure or an error are another. Has the a
by worik 5y ago
It has been many years since I used C++
But returning null is an obvious choice.
Combined types which return the structure or an error are another. Has the advantage of returning error information, why failure.
There are many ways
- roger10-4 5y agoYou can’t return null from a constructor (constructors have no return)
- worik 5y agoWhat dumb design. I thought the constructor returned a pointer to the memory created. No return? Stupid!
- gpderetta 5y agoFWIW, constructors do not "create" (I assume you mean allocate) memory. That's the job of operator new. A constructor, given a block of untyped memory, will construct an object in it.
- worik 5y agoRight. Been a long time. I would still expect a constructor to return. How can you tell if it failed?
- Calavar 5y ago> But returning null is an obvious choice. It's not because C++ is not an everything-is-a-reference language. > Combined types which return the structure or an error are another. Has the advantage of returning error information, why failure. Sum types are great, but changing constructors to return sum types would break all existing code. It would also lead to a whole slew of questions about how the construction of arrays of values would work. (Does an array of an objects now turn into an array of wrapper types? Byebye SIMD optimization!) It would also break the consistency of constructing POD vs non POD types, which would make writing templates that need to generalize over both a huge PITA.