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

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

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

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

Herb Sutter, Andrei Alexandrescu

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

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

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


Discussion


Implicit conversions have two main problems:

  • They can fire in the most unexpected places.

  • They don't always mesh well with the rest of the language.


Implicitly converting constructors (constructors that can be called with one argument and are not declared

explicit ) interact poorly with overloading and foster invisible temporary objects that pop up all over. Conversions defined as member functions of the form

operator T (where

T is a type) are no betterthey interact poorly with implicit constructors and can allow all sorts of nonsensical code to compile. Examples are embarrassingly numerous (see References). We mention only two (see Examples).Item 54):

class Widget {

// …

explicit Widget( unsigned int widgetizationFactor );

explicit Widget( const char* name, const Widget* other = 0 );
};

  • Use named functions that offer conversions instead of conversion operators:

    class String {

    // …

    const char* as_char_pointer() const;

    // in the grand c_str tradition
    };


  • See also the discussion of

    explicit copy constructors in Item 54.


    / 521