tailieunhanh - Thinking in C plus plus (P12)

Tham khảo tài liệu 'thinking in c plus plus (p12)', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Write the contents to an ostream void print std ostream os const os 0x std hex int b std dec endif BYTE_H C12 include include fstream using namespace std ofstream out void k Byte bl Byte b2 bl bl b2 b2 bl define TRY2 OP out bl out out b2 out out bl OP b2 produces bl OP b2 .print out out endl bl 9 b2 47 TRY2 TRY2 - TRY2 TRY2 TRY2 TRY2 TRY2 TRY2 TRY2 TRY2 TRY2 TRY2 - TRY2 TRY2 TRY2 TRY2 TRY2 TRY2 TRY2 TRY2 TRY2 Assignment operator Conditionals define TRYC2 OP out bl out out b2 out out bl OP b2 produces out bl OP b2 out endl bl 9 b2 47 TRYC2 TRYC2 TRYC2 TRYC2 TRYC2 TRYC2 TRYC2 TRYC2 Chained assignment Byte b3 92 530 Thinking in C www. BruceEckel .com bl b2 b3 int main out member functions endl Byte b1 47 b2 9 k b1 b2 You can see that operator is only allowed to be a member function. This is explained later. Notice that all of the assignment operators have code to check for self-assignment this is a general guideline. In some cases this is not necessary for example with operator you often want to say A A and have it add A to itself. The most important place to check for self-assignment is operator because with complicated objects disastrous results may occur. In some cases it s OK but you should always keep it in mind when writing operators All of the operators shown in the previous two examples are overloaded to handle a single type. It s also possible to overload operators to handle mixed types so you can add apples to oranges for example. Before you start on an exhaustive overloading of operators however you should look at the section on automatic type conversion later in this chapter. Often a type conversion in the right place can save you a lot of overloaded operators. Arguments return values It may seem a little confusing at first when you look at and see all the different ways that arguments are passed and returned. Although you can pass and .