1

I am reading Bjarne Stroustrup's book titled "A tour of C++".

The following section is confusing to me:

When a class is a resource handle – that is, when the class is responsible for an object accessed through a pointer – the default member wise copy is typically a disaster.

Member wise copy would violate the resource handle’s invariant. For example, the default copy would leave a copy of a Vector referring to the same elements as the original:

void bad_copy(Vector v1)
{
    Vector v2 = v1;
    // copy v1’s representation into v2
    v1[0] = 2;
    // v2[0] is now also 2!
    v2[1] = 3;
    // v1[1] is now also 3!
}

In particular, I do not understand the following:

  1. When Vector v1 is passed as a value, not as a reference, how come v2 and v1 both refer to the same memory address.

  2. What does the sentence "When a class is a resource handle" means ?

3

What does the sentence "When a class is a resource handle" mean?

It means that the classes members are not actually the interesting data, but only pointers, references or other kinds of handles for it.
Thus, copying them does not copy the interesting data, meaning a member-by-member-copy is flat-out wrong.

Which also answers your other question.

For ensuring that you copy things right, take a look at "the rule of three" and its evolution "the rule of five".
Though if you have the choice (which is nearly always), follow "the rule of zero".

2

I am going to answer in opposite order, to make it clearer.


  1. What does the sentence "When a class is a resource handle" means ?

A resource can be anything, from piece of memory, up to a file. Handling a resource means, opening a resource, providing access to it, closing it at the end.

For example, next class is going to allocate some memory in the constructor, release it in destructor, and provide a method to access it (I used c functions on purpose) :

class MemoryHandler
{
public:
    MemoryHandler() : mem( malloc(10) )
    {
    }
    ~MemoryHandler()
    {
        free(mem);
    }
    void* Access(){ return mem;}
    void* mem;
};

So, what happens when you copy such object? If you are lucky, the program will crash.

void DoSomething( MemoryHandler m )
{
  // do something
}

MemoryHandler m;
DoSomething( m );

Why it will crash? Because the function DoSomething will take the object by value, i.e. will create a new object using copy constructor, which is going to be released at the end of the scope. The default copy constructor will just blindly copy the pointer. Therefore, the cause of the crash will be double memory release.

The default copy constructor looks like this :

MemoryHandler( const MemoryHandler& object ) : mem( object.mem )
{
}

How to solve this particular problem, depends on what you need. See std::shared_ptr if you want to make the class copyable, or std::unique_ptr to make your class movable.


  1. When Vector v1 is passed as a value, not as a reference, how come v2 and v1 both refer to the same memory address.

You didn't say what vector is, but I assume that it looks something like in the above example.

To solve your problem, learn what Rule of five is, and apply it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.