Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6895901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:58:04+00:00 2026-05-27T06:58:04+00:00

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

  • 0

The code below was copied from Bruce Eckel’s 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’m having some trouble understanding the following comment about the class exception RangeError:

“The PWrap template shows a more typical use of exceptions than you’ve seen so far: A
nested class called RangeError is created to use in operator[ ] if its argument is out of range.
Because operator[ ] returns a reference it cannot return zero. (There are no null references.)
This is a true exceptional condition – you don’t know what to do in the current context, and
you can’t return an improbable value.”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:58:04+00:00Added an answer on May 27, 2026 at 6:58 am

    If the function were to return a pointer rather than a reference, than it could signal failure (i.e. out-of-bounds index) by returning a NULL pointer. But you can’t have NULL references, so the only option available is to throw an exception.*

    As @Steve points out in comments below, you wouldn’t want operator[] to return a pointer, because that would mean you’d need to write something like:

    T x = *wrapper[5];
    

    * An alternative would be to assert.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

THe code below I copied from MSDN with a bit of modification: [DllImport(user32.dll, CharSet
Everything works fine. My code as below copied from Google's guide. My problem is,
I can get the list of country names using below code, (copied from somewhere
I have the following simple code (see below) I copied from a book. But
This is some sample code copied from The C++ Programming Language chapter 17 as
I'm using the below code to transfer an image from one folder on external
I have some code copied from the fitness website: package fixtures; import static fitnesse.util.ListUtility.list;
I have this code below to copy VBA codes from one word document to
Background We've been using some code copied verbatim from Joe Duffy's Concurrent Programming on
I used the code below to copy from one binary file to another, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.