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

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

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

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

Herb Sutter, Andrei Alexandrescu

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

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

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


Discussion


In general, for some binary operator

@ (be it

+, -, * , and so on), you should define its assignment version such that

a @= b and

a = a @ b have the same meaning (other than that the first form might be more efficient and only evaluates

a once). The canonical way of achieving this goal is to define

@ in terms of

@= , as follows:

T& T::operator@=( const T& ) {

// … implementation …
return *this;
}
T operator@( const T& lhs, const T& rhs ) {
T temp( lhs );
return temp @= rhs;
}

The two functions work in tandem. The assignment form does the actual work and returns its left-hand parameter. The non-assignment version creates a temporary from

lhs , modifies it by invoking the assignment form, and returns it.Item 44.) For example, if you define a class

String that has an implicit constructor taking a

char , making

operator+( const String&, const String& ) a nonmember enables both

char + String and

String + char to work; a member version

String::operator+( const String& ) would only accept the latter. An efficiency-minded implementation might choose to define several nonmember overloads of

operator@ to avoid proliferation of temporaries resulted through conversions (see Item 29).Item 44). In any case, put all nonmember operators in the same namespace as

T so that they will be conveniently available to callers as well as to avoid name lookup surprises (see Item 57).

A variation is to have

operator@ accept its first parameter by value. This way, you arrange for the compiler itself to perform the copy for you implicitly, and this can give the compiler more leeway in applying optimizations:

T& operator@=(

T& lhs, const T& rhs ) {

// … implementation …
return lhs;
}
T operator@(

T lhs , const T& rhs ) {

// lhs taken by value
return lhs @= rhs;
}

Another variation is to have

operator@ return a

const value. This technique has the advantage that it disables nonsensical code such as

a + b = c , but it does so at the cost of disabling some potentially useful constructs such as

a = (b + c).replace(pos, n, d) expressive code that, in one shot, concatenates strings

b and

c , replaces some characters, and assigns the final result to

a .


/ 521