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

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

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

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

Herb Sutter, Andrei Alexandrescu

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

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

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


Examples


Example 1: Overloading.
Say you have a

Widget::Widget( unsigned int ) that can be invoked implicitly, and a

Display function overloaded for

Widget s and

double s. Consider the following overload resolution surprise:

void Display( double );

// displays a double
void Display( const Widget& );

// displays a Widget
Display( 5 );

// oops: creates and displays a Widget

Example 2: Errors that work.
Say you provide

operator const char* for a

String class:

class String {

// …
public:
operator const char*();

// deplorable form
};

Suddenly, a lot of silly expressions now compile. Assume

s1, s2 are

String s:

int x = s1 - s2;

// compiles; undefined behavior
const char* p = s1 - 5;

// compiles; undefined behavior
p = s1 + '0';

// compiles; doesn't do what you'd expect
if( s1 == "0" ) { ...}

// compiles; doesn't do what you'd expect

The standard

string wisely avoids an

operator const char* for exactly this reason.


/ 521