Читаем C++ Primer Plus полностью

total = coding + fixing;              // operator notation

Either notation invokes the operator+() method. Note that with the operator notation, the object to the left of the operator (coding, in this case) is the invoking object, and the object to the right (fixing, in this case) is the one passed as an argument. Listing 11.6 illustrates this point.

Listing 11.6. usetime1.cpp

// usetime1.cpp -- using the second draft of the Time class

// compile usetime1.cpp and mytime1.cpp together

#include

#include "mytime1.h"

int main()

{

    using std::cout;

    using std::endl;

    Time planning;

    Time coding(2, 40);

    Time fixing(5, 55);

    Time total;

    cout << "planning time = ";

    planning.Show();

    cout << endl;

    cout << "coding time = ";

    coding.Show();

    cout << endl;

    cout << "fixing time = ";

    fixing.Show();

    cout << endl;

    total = coding + fixing;

    // operator notation

    cout << "coding + fixing = ";

    total.Show();

    cout << endl;

    Time morefixing(3, 28);

    cout << "more fixing time = ";

    morefixing.Show();

    cout << endl;

    total = morefixing.operator+(total);

    // function notation

    cout << "morefixing.operator+(total) = ";

    total.Show();

    cout << endl;

    return 0;

}

Here is the output of the program in Listings 11.4, 11.5, and 11.6:

planning time = 0 hours, 0 minutes

coding time = 2 hours, 40 minutes

fixing time = 5 hours, 55 minutes

coding + fixing = 8 hours, 35 minutes

more fixing time = 3 hours, 28 minutes

morefixing.operator+(total) = 12 hours, 3 minutes

In short, the name of the operator+() function allows it to be invoked by using either function notation or operator notation. The compiler uses the operand types to figure out what to do:

int a, b, c;

Time A, B, C;

c = a + b;      // use int addition

C = A + B;      // use addition as defined for Time objects

Can you add more than two objects? For example, if t1, t2, t3, and t4 are all Time objects, can you do the following?

t4 = t1 + t2 + t3;                     // valid?

The way to answer this is to consider how the statement gets translated into function calls. Because addition is a left-to-right operator, the statement is first translated to this:

t4 = t1.operator+(t2 + t3);            // valid?

Then the function argument is itself translated to a function call, giving the following:

t4 = t1.operator+(t2.operator+(t3));   // valid? YES

Is this valid? Yes, it is. The function call t2.operator+(t3) returns a Time object that represents the sum of t2 and t3. This object then becomes the object of the t1.operator+() function call, and that call returns the sum of t1 and the Time object that represents the sum of t2 and t3. In short, the final return value is the sum of t1, t2, and t3, just as desired.

Overloading Restrictions

Most C++ operators (see Table 11.1) can be overloaded in the manner described in the preceding section. Overloaded operators (with some exceptions) don’t necessarily have to be member functions. However, at least one of the operands has to be a user-defined type. Let’s take a closer look at the limits C++ imposes on user-defined operator overloading:

• The overloaded operator must have at least one operand that is a user-defined type. This prevents you from overloading operators for the standard types. Thus, you can’t redefine the minus operator (-) so that it yields the sum of two double values instead of their difference. This restriction preserves program sanity, although it may hinder creative accounting.

• You can’t use an operator in a manner that violates the syntax rules for the original operator. For example, you can’t overload the modulus operator (%) so that it can be used with a single operand:

int x;

Time shiva;

% x;       // invalid for modulus operator

% shiva;   // invalid for overloaded operator

Similarly, you can’t alter operator precedence. So if you overload the addition operator to let you add two classes, the new operator has the same precedence as ordinary addition.

• You can’t create new operator symbols. For example, you can’t define an operator**() function to denote exponentiation.

• You cannot overload the following operators:

This still leaves all the operators in Table 11.1 available for overloading.

• Most of the operators in Table 11.1 can be overloaded by using either member or nonmember functions. However, you can use only member functions to overload the following operators:

Table 11.1. Operators That Can Be Overloaded

Note

This chapter does not cover every operator mentioned in the list of restrictions or in Table 11.1. However, Appendix E, “Other Operators,” summarizes the operators that are not covered in the main body of this text.

In addition to these formal restrictions, you should use sensible restraint in overloading operators. For example, you shouldn’t overload the * operator so that it swaps the data members of two Time objects. Nothing in the notation would suggest what the operator did, so it would be better to define a class method with an explanatory name such as Swap().

Перейти на страницу:

Все книги серии Developer's Library

C++ Primer Plus
C++ Primer Plus

C++ Primer Plus is a carefully crafted, complete tutorial on one of the most significant and widely used programming languages today. An accessible and easy-to-use self-study guide, this book is appropriate for both serious students of programming as well as developers already proficient in other languages.The sixth edition of C++ Primer Plus has been updated and expanded to cover the latest developments in C++, including a detailed look at the new C++11 standard.Author and educator Stephen Prata has created an introduction to C++ that is instructive, clear, and insightful. Fundamental programming concepts are explained along with details of the C++ language. Many short, practical examples illustrate just one or two concepts at a time, encouraging readers to master new topics by immediately putting them to use.Review questions and programming exercises at the end of each chapter help readers zero in on the most critical information and digest the most difficult concepts.In C++ Primer Plus, you'll find depth, breadth, and a variety of teaching techniques and tools to enhance your learning:• A new detailed chapter on the changes and additional capabilities introduced in the C++11 standard• Complete, integrated discussion of both basic C language and additional C++ features• Clear guidance about when and why to use a feature• Hands-on learning with concise and simple examples that develop your understanding a concept or two at a time• Hundreds of practical sample programs• Review questions and programming exercises at the end of each chapter to test your understanding• Coverage of generic C++ gives you the greatest possible flexibility• Teaches the ISO standard, including discussions of templates, the Standard Template Library, the string class, exceptions, RTTI, and namespaces

Стивен Прата

Программирование, программы, базы данных

Похожие книги

1С: Бухгалтерия 8 с нуля
1С: Бухгалтерия 8 с нуля

Книга содержит полное описание приемов и методов работы с программой 1С:Бухгалтерия 8. Рассматривается автоматизация всех основных участков бухгалтерии: учет наличных и безналичных денежных средств, основных средств и НМА, прихода и расхода товарно-материальных ценностей, зарплаты, производства. Описано, как вводить исходные данные, заполнять справочники и каталоги, работать с первичными документами, проводить их по учету, формировать разнообразные отчеты, выводить данные на печать, настраивать программу и использовать ее сервисные функции. Каждый урок содержит подробное описание рассматриваемой темы с детальным разбором и иллюстрированием всех этапов.Для широкого круга пользователей.

Алексей Анатольевич Гладкий

Программирование, программы, базы данных / Программное обеспечение / Бухучет и аудит / Финансы и бизнес / Книги по IT / Словари и Энциклопедии
1С: Управление торговлей 8.2
1С: Управление торговлей 8.2

Современные торговые предприятия предлагают своим клиентам широчайший ассортимент товаров, который исчисляется тысячами и десятками тысяч наименований. Причем многие позиции могут реализовываться на разных условиях: предоплата, отсрочка платежи, скидка, наценка, объем партии, и т.д. Клиенты зачастую делятся на категории – VIP-клиент, обычный клиент, постоянный клиент, мелкооптовый клиент, и т.д. Товарные позиции могут комплектоваться и разукомплектовываться, многие товары подлежат обязательной сертификации и гигиеническим исследованиям, некондиционные позиции необходимо списывать, на складах периодически должна проводиться инвентаризация, каждая компания должна иметь свою маркетинговую политику и т.д., вообщем – современное торговое предприятие представляет живой организм, находящийся в постоянном движении.Очевидно, что вся эта кипучая деятельность требует автоматизации. Для решения этой задачи существуют специальные программные средства, и в этой книге мы познакомим вам с самым популярным продуктом, предназначенным для автоматизации деятельности торгового предприятия – «1С Управление торговлей», которое реализовано на новейшей технологической платформе версии 1С 8.2.

Алексей Анатольевич Гладкий

Финансы / Программирование, программы, базы данных