C++.Coding.Standards.1918.Rules.Guidelines [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

C++.Coding.Standards.1918.Rules.Guidelines [Electronic resources] - نسخه متنی

Herb Sutter, Andrei Alexandrescu

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید


Discussion


A common mistake (and not only among beginners) is to forget to think about copy and assignment semantics when defining a class. This happens often with small helper classes such as those meant for RAII support (see Item 13).

Ensure that your class provides sensible copying, or none at all. The choices are:

  • Explicitly disable both:
    If copying doesn't make sense for your type, disable both copy construction and copy assignment by declaring them as private unimplemented functions:

    class T {

    // …
    private:

    // make T non-copyable
    T( const T& );

    // not implemented
    T& operator=( const T& );

    // not implemented
    };

  • Explicitly write both:
    If copying and copy assignment is warranted for

    T objects, but correct copying behavior differs from what the compiler-generated versions will do, then write the functions yourself and make them nonprivate.

  • Use the compiler-generated versions, preferably with an explicit comment:
    Otherwise, if copying makes sense and the default behavior is correct, don't declare them yourself and just let the compiler-generated versions kick in. Prefer to comment that the default behavior is correct, so that readers of your code will know that you didn't miss one of the other two options by accident.


Note that disabling copying and copy assignment means that you cannot put

T objects into standard containers. That's not necessarily a bad thing; very likely, you wouldn't want to hold such

T objects in a container anyway. (You can still put them in containers by holding them via a smart pointer; see Item 79.)Item 32.)


/ 521