Why does the author emphasize the fact that the operator [] cannot return 0?

The code below was copied from Bruce Eckel Thinking in C ++ Volume 2 Chapter 16

//: C07:Wrapped.cpp // From Thinking in C++, 2nd Edition // Available at http://www.BruceEckel.com // (c) Bruce Eckel 2000 // Copyright notice in Copyright.txt // Safe, atomic pointers #include <fstream> #include <cstdlib> using namespace std; ofstream out("wrapped.out"); // Simplified. Yours may have other arguments. template<class T, int sz = 1> class PWrap { T* ptr; public: class RangeError {}; // Exception class PWrap() { ptr = new T[sz]; out << "PWrap constructor" << endl; } ~PWrap() { delete []ptr; out << "PWrap destructor" << endl; } T& operator[](int i) throw(RangeError) { if(i >= 0 && i < sz) return ptr[i]; throw RangeError(); } }; class Cat { public: Cat() { out << "Cat()" << endl; } ~Cat() { out << "~Cat()" << endl; } void g() {} }; class Dog { public: void* operator new[](size_t sz) { out << "allocating a Dog" << endl; throw int(47); } void operator delete[](void* p) { out << "deallocating a Dog" << endl; ::delete p; } }; class UseResources { PWrap<Cat, 3> Bonk; PWrap<Dog> Og; public: UseResources() : Bonk(), Og() { out << "UseResources()" << endl; } ~UseResources() { out << "~UseResources()" << endl; } void f() { Bonk[1].g(); } }; int main() { try { UseResources ur; } catch(int) { out << "inside handler" << endl; } catch(...) { out << "inside catch(...)" << endl; } } 

I have no problem with the code itself. But I had trouble understanding the following comment about RangeError class RangeError :

"The PWrap template shows a more typical use of exceptions than you have seen so far: A nested class called RangeError is created for use in operator[ ] if its argument is out of range. Since operator[ ] returns a link, it cannot return null. (There are no null references.) This is a true exceptional condition - you don’t know what to do in the current context, and you cannot return an incredible value. "

+4
source share
4 answers

If the function was to return a pointer, not a link, then it could signal an error (i.e., an index outside the bounds), returning a NULL pointer. But you cannot have NULL links, so the only option available is an exception. *

As @Steve points out in the comments below, you do not want operator[] return a pointer, because that would mean you need to write something like:

 T x = *wrapper[5]; 


* An alternative might be assert .
+7
source

He emphasizes this to explain why throwing an exception is the only option in this case.

If the operator returned a pointer, it could return a null pointer instead of throwing an exception in case of errors. But since it returns a link and there is no such thing as a null link, the only way to handle errors is to throw an exception.

+3
source

The author explains this quite well, perhaps the null part consumes that it means that it cannot return any value that means there is no value as such (for example, a null pointer) and therefore throws it away.

+1
source

The author said this: "Because the [] operator returns a link, it cannot return zero." Because the NULL link is very bad.

0
source

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


All Articles