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 ?

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”.

0

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật