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

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

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

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

Herb Sutter, Andrei Alexandrescu

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

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

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


Discussion


Both public member functions and nonmember functions form part of the public interface of a class. The Interface Principle states: For a class

X , all functions (including nonmember functions) that both "mention"

X and are "supplied with"

X in the same namespace are logically part of

X , because they form part of

X 's interface. (See Item 44 and [Sutter00].)Sutter00]).

Consider a class

X , defined in namespace

N :

class X {
public:
void f();
};
X operator+( const X&, const X& );

Callers will typically want to write code like this, where

x1, x2 , and

x3 are objects of type

X :

x3 = x1 + x2;

If the

operator+ is declared in the same namespace as

X , there's no problem, and such code always just works, because the supplied

operator+ will be looked up using ADL.

If the

operator+ is not declared in the same namespace as

X , the caller's code fails to work. The caller has two workarounds to make it work. The first is to use explicit qualification:

x3 = N::operator+( x1, x2 );

This is deplorable and shameful because it requires the user to give up natural operator syntax, which is the point of operator overloading in the first place. The only other option is to write a

using statement:

using N::operator+;

// or: using namespace N;
x3 = x1 + x2;

Writing either of the indicated

using statements is perfectly acceptable (see Item 59), but the caller doesn't have to jump through these hoops when the author of

X does the right thing and puts

operator+ for

X objects into the same namespace as

X .

For the flip side of this issue, see Item 58).


/ 521