Главная » Просмотр файлов » B. Stroustrup - The C++ Programming Language

B. Stroustrup - The C++ Programming Language (794319), страница 19

Файл №794319 B. Stroustrup - The C++ Programming Language (B. Stroustrup - The C++ Programming Language) 19 страницаB. Stroustrup - The C++ Programming Language (794319) страница 192019-05-09СтудИзба
Просмтор этого файла доступен только зарегистрированным пользователям. Но у нас супер быстрая регистрация: достаточно только электронной почты!

Текст из файла (страница 19)

The majority of C++ constructs arededicated to the design and implementation of elegant and efficient abstractions (e.g., user-definedtypes and algorithms using them). One effect of this modularity and abstraction (in particular, theuse of libraries) is that the point where a run-time error can be detected is separated from the pointwhere it can be handled. As programs grow, and especially when libraries are used extensively,standards for handling errors become important.2.4.3.1 ExceptionsConsider again the Vector example. What ought to be done when we try to access an element thatis out of range for the vector from §2.3.2?• The writer of Vector doesn’t know what the user would like to have done in this case (thewriter of Vector typically doesn’t even know in which program the vector will be running).• The user of Vector cannot consistently detect the problem (if the user could, the out-of-rangeaccess wouldn’t happen in the first place).The solution is for the Vector implementer to detect the attempted out-of-range access and then tellthe user about it.

The user can then take appropriate action. For example, Vector::operator[]() candetect an attempted out-of-range access and throw an out_of_range exception:double& Vector::operator[](int i){if (i<0 || size()<=i) throw out_of_range{"Vector::operator[]"};return elem[i];}The throw transfers control to a handler for exceptions of type out_of_range in some function thatdirectly or indirectly called Vector::operator[]().

To do that, the implementation will unwind thefunction call stack as needed to get back to the context of that caller (§13.5.1). For example:void f(Vector& v){// ...try { // exceptions here are handled by the handler defined belowv[v.size()] = 7; // try to access beyond the end of v}catch (out_of_range) { // oops: out_of_range error// ...

handle range error ...}// ...}We put code for which we are interested in handling exceptions into a try-block. That attemptedassignment to v[v.size()] will fail. Therefore, the catch-clause providing a handler for out_of_rangewill be entered. The out_of_range type is defined in the standard library and is in fact used by somestandard-library container access functions.Use of the exception-handling mechanisms can make error handling simpler, more systematic,and more readable. See Chapter 13 for further discussion, details, and examples.56A Tour of C++: The BasicsChapter 22.4.3.2 InvariantsThe use of exceptions to signal out-of-range access is an example of a function checking its argument and refusing to act because a basic assumption, a precondition, didn’t hold.

Had we formallyspecified Vector’s subscript operator, we would have said something like ‘‘the index must be in the[0:size()) range,’’ and that was in fact what we tested in our operator[](). Whenever we define afunction, we should consider what its preconditions are and if feasible test them (see §12.4, §13.4).However, operator[]() operates on objects of type Vector and nothing it does makes any senseunless the members of Vector have ‘‘reasonable’’ values.

In particular, we did say ‘‘elem points toan array of sz doubles’’ but we only said that in a comment. Such a statement of what is assumedto be true for a class is called a class invariant, or simply an invariant. It is the job of a constructorto establish the invariant for its class (so that the member functions can rely on it) and for the member functions to make sure that the invariant holds when they exit. Unfortunately, our Vector constructor only partially did its job.

It properly initialized the Vector members, but it failed to checkthat the arguments passed to it made sense. Consider:Vector v(−27);This is likely to cause chaos.Here is a more appropriate definition:Vector::Vector(int s){if (s<0) throw length_error{};elem = new double[s];sz = s;}I use the standard-library exception length_error to report a non-positive number of elementsbecause some standard-library operations use that exception to report problems of this kind. Ifoperator new can’t find memory to allocate, it throws a std::bad_alloc. We can now write:void test(){try {Vector v(−27);}catch (std::length_error) {// handle negative size}catch (std::bad_alloc) {// handle memory exhaustion}}You can define your own classes to be used as exceptions and have them carry arbitrary informationfrom a point where an error is detected to a point where it can be handled (§13.5).Often, a function has no way of completing its assigned task after an exception is thrown.Then, ‘‘handling’’ an exception simply means doing some minimal local cleanup and rethrowingthe exception (§13.5.2.1).Section 2.4.3.2Invariants57The notion of invariants is central to the design of classes, and preconditions serve a similar rolein the design of functions.

Invariants• helps us to understand precisely what we want• forces us to be specific; that gives us a better chance of getting our code correct (afterdebugging and testing).The notion of invariants underlies C++’s notions of resource management supported by constructors (§2.3.2) and destructors (§3.2.1.2, §5.2). See also §13.4, §16.3.1, and §17.2.2.4.3.3 Static AssertionsExceptions report errors found at run time. If an error can be found at compile time, it is usuallypreferable to do so. That’s what much of the type system and the facilities for specifying the interfaces to user-defined types are for.

However, we can also perform simple checks on other properties that are known at compile time and report failures as compiler error messages. For example:static_assert(4<=sizeof(int), "integers are too small"); // check integer sizeThis will write integers are too small if 4<=sizeof(int) does not hold, that is, if an int on this systemdoes not have at least 4 bytes.

We call such statements of expectations assertions.The static_assert mechanism can be used for anything that can be expressed in terms of constantexpressions (§2.2.3, §10.4). For example:constexpr double C = 299792.458;// km/svoid f(double speed){const double local_max = 160.0/(60∗60);// 160 km/h == 160.0/(60*60) km/sstatic_assert(speed<C,"can't go that fast");static_assert(local_max<C,"can't go that fast");// error : speed must be a constant// OK// ...}In general, static_assert(A,S) prints S as a compiler error message if A is not true.The most important uses of static_assert come when we make assertions about types used asparameters in generic programming (§5.4.2, §24.3).For runtime-checked assertions, see §13.4.2.5 PostscriptThe topics covered in this chapter roughly correspond to the contents of Part II (Chapters 6–15).Those are the parts of C++ that underlie all programming techniques and styles supported by C++.Experienced C and C++ programmers, please note that this foundation does not closely correspondto the C or C++98 subsets of C++ (that is, C++11).58A Tour of C++: The Basics2.6 Advice[1][2][3]Don’t panic! All will become clear in time; §2.1.You don’t have to know every detail of C++ to write good programs; §1.3.1.Focus on programming techniques, not on language features; §2.1.Chapter 23A Tour of C++: Abstraction MechanismsDon’t Panic!– Douglas Adams•••••IntroductionClassesConcrete Types; Abstract Types; Virtual Functions; Class HierarchiesCopy and MoveCopying Containers; Moving Containers; Resource Management; Suppressing OperationsTemplatesParameterized Types; Function Templates; Function Objects; Variadic Templates; AliasesAdvice3.1 IntroductionThis chapter aims to give you an idea of C++’s support for abstraction and resource managementwithout going into a lot of detail.

It informally presents ways of defining and using new types(user-defined types). In particular, it presents the basic properties, implementation techniques, andlanguage facilities used for concrete classes, abstract classes, and class hierarchies. Templates areintroduced as a mechanism for parameterizing types and algorithms with (other) types and algorithms. Computations on user-defined and built-in types are represented as functions, sometimesgeneralized to template functions and function objects. These are the language facilities supportingthe programming styles known as object-oriented programming and generic programming.

Thenext two chapters follow up by presenting examples of standard-library facilities and their use.The assumption is that you have programmed before. If not, please consider reading a textbook, such as Programming: Principles and Practice Using C++ [Stroustrup,2009], before continuing here. Even if you have programmed before, the language you used or the applications youwrote may be very different from the style of C++ presented here.

Характеристики

Тип файла
PDF-файл
Размер
18,76 Mb
Тип материала
Высшее учебное заведение

Список файлов книги

Свежие статьи
Популярно сейчас
Зачем заказывать выполнение своего задания, если оно уже было выполнено много много раз? Его можно просто купить или даже скачать бесплатно на СтудИзбе. Найдите нужный учебный материал у нас!
Ответы на популярные вопросы
Да! Наши авторы собирают и выкладывают те работы, которые сдаются в Вашем учебном заведении ежегодно и уже проверены преподавателями.
Да! У нас любой человек может выложить любую учебную работу и зарабатывать на её продажах! Но каждый учебный материал публикуется только после тщательной проверки администрацией.
Вернём деньги! А если быть более точными, то автору даётся немного времени на исправление, а если не исправит или выйдет время, то вернём деньги в полном объёме!
Да! На равне с готовыми студенческими работами у нас продаются услуги. Цены на услуги видны сразу, то есть Вам нужно только указать параметры и сразу можно оплачивать.
Отзывы студентов
Ставлю 10/10
Все нравится, очень удобный сайт, помогает в учебе. Кроме этого, можно заработать самому, выставляя готовые учебные материалы на продажу здесь. Рейтинги и отзывы на преподавателей очень помогают сориентироваться в начале нового семестра. Спасибо за такую функцию. Ставлю максимальную оценку.
Лучшая платформа для успешной сдачи сессии
Познакомился со СтудИзбой благодаря своему другу, очень нравится интерфейс, количество доступных файлов, цена, в общем, все прекрасно. Даже сам продаю какие-то свои работы.
Студизба ван лав ❤
Очень офигенный сайт для студентов. Много полезных учебных материалов. Пользуюсь студизбой с октября 2021 года. Серьёзных нареканий нет. Хотелось бы, что бы ввели подписочную модель и сделали материалы дешевле 300 рублей в рамках подписки бесплатными.
Отличный сайт
Лично меня всё устраивает - и покупка, и продажа; и цены, и возможность предпросмотра куска файла, и обилие бесплатных файлов (в подборках по авторам, читай, ВУЗам и факультетам). Есть определённые баги, но всё решаемо, да и администраторы реагируют в течение суток.
Маленький отзыв о большом помощнике!
Студизба спасает в те моменты, когда сроки горят, а работ накопилось достаточно. Довольно удобный сайт с простой навигацией и огромным количеством материалов.
Студ. Изба как крупнейший сборник работ для студентов
Тут дофига бывает всего полезного. Печально, что бывают предметы по которым даже одного бесплатного решения нет, но это скорее вопрос к студентам. В остальном всё здорово.
Спасательный островок
Если уже не успеваешь разобраться или застрял на каком-то задание поможет тебе быстро и недорого решить твою проблему.
Всё и так отлично
Всё очень удобно. Особенно круто, что есть система бонусов и можно выводить остатки денег. Очень много качественных бесплатных файлов.
Отзыв о системе "Студизба"
Отличная платформа для распространения работ, востребованных студентами. Хорошо налаженная и качественная работа сайта, огромная база заданий и аудитория.
Отличный помощник
Отличный сайт с кучей полезных файлов, позволяющий найти много методичек / учебников / отзывов о вузах и преподователях.
Отлично помогает студентам в любой момент для решения трудных и незамедлительных задач
Хотелось бы больше конкретной информации о преподавателях. А так в принципе хороший сайт, всегда им пользуюсь и ни разу не было желания прекратить. Хороший сайт для помощи студентам, удобный и приятный интерфейс. Из недостатков можно выделить только отсутствия небольшого количества файлов.
Спасибо за шикарный сайт
Великолепный сайт на котором студент за не большие деньги может найти помощь с дз, проектами курсовыми, лабораторными, а также узнать отзывы на преподавателей и бесплатно скачать пособия.
Популярные преподаватели
Добавляйте материалы
и зарабатывайте!
Продажи идут автоматически
6417
Авторов
на СтудИзбе
307
Средний доход
с одного платного файла
Обучение Подробнее