Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

ANSI/ISO C++ Professional Programmer''''s Handbook phần 3 ppt
Nội dung xem thử
Mô tả chi tiết
Secondly, a conversion operator takes no arguments.
Conversion operators can convert their object to any given type, fundamental and user-defined alike:
struct DateRep //legacy C code
{
char day;
char month;
short year;
};
class Date // object-oriented wrapper
{
private:
DateRep dr;
public:
operator DateRep () const { return dr;} // automatic conversion to DateRep
};
extern "C" int transmit_date(DateRep); // C-based communication API function
int main()
{
Date d;
//...use d
//transmit date object as a binary stream to a remote client
int ret_stat = transmit_date; //using legacy communication API
return 0;
}
Standard Versus User-Defined Conversions
The interaction of a user-defined conversion with a standard conversion can cause undesirable surprises and side
effects, and therefore must be used with caution. Examine the following concrete example.
A non-explicit constructor that takes a single argument is also a conversion operator, which casts its argument to
an object of this class. When the compiler has to resolve an overloaded function call, it takes into consideration such
user-defined conversions in addition to the standard ones. For example
class Numeric
{
private:
float f;
public:
Numeric(float ff): f(ff) {} //constructor is also a float-to-Numeric
// conversion operator
};
void f(Numeric);
Numeric num(0.05);
f(5.f); //OK, calls void f(Numeric). Numeric's constructor
//converts argument to a Numeric object
'Suppose you add, at a later stage, another overloaded version of f():
ANSI/ISO C++ Professional Programmer's Handbook - Chapter 3 - Operator Overloading
file:///D|/Cool Stuff/old/ftp/1/1/ch03/ch03.htm (8 von 15) [12.05.2000 14:45:47]
void f (double);
Now the same function call resolves differently:
f(5.f); // now calls f(double), not f(Numeric)
This is because float is promoted to double automatically in order to match an overloaded function signature.
This is a standard type conversion. On the other hand, the conversion of float to Numeric is a user-defined
conversion. User-defined conversions rank lower than standard conversions -in overload resolution; as a result, the
function call resolves differently.
Because of this phenomenon and others, conversion operators have been severely criticized. Some programming
schools ban their usage altogether. However, conversion operators are a valuable -- and sometimes inevitable -- tool
for bridging between dual interfaces, as you have seen.
Postfix and Prefix Operators
For primitive types, C++ distinguishes between ++x; and x++; as well as between --x; and x--;. Under some
circumstances, objects have to distinguish between prefix and postfix overloaded operators as well (for example, as an
optimization measure. See Chapter 12, "Optimizing Your Code"). Postfix operators are declared with a dummy int
argument, whereas their prefix counterparts take no arguments. For example
class Date
{
public:
Date& operator++(); //prefix
Date& operator--(); //prefix
Date& operator++(int unused); //postfix
Date& operator--(int unused); //postfix
};
void f()
{
Date d, d1;
d1 = ++d;//prefix: first increment d and then assign to d1
d1 = d++; //postfix; first assign, increment d afterwards
}
Using Function Call Syntax
An overloaded operator call is merely "syntactic sugar" for an ordinary function call. You can use the explicit
function call instead of the operator syntax as follows:
bool operator==(const Date& d1, const Date& d2);
void f()
{
Date d, d1;
bool equal;
d1.operator++(0); // equivalent to: d1++;
d1.operator++(); // equivalent to: ++d1;
equal = operator==(d, d1);// equivalent to: d==d1;
ANSI/ISO C++ Professional Programmer's Handbook - Chapter 3 - Operator Overloading
file:///D|/Cool Stuff/old/ftp/1/1/ch03/ch03.htm (9 von 15) [12.05.2000 14:45:47]