09 2021 档案
摘要:Virtual functions are dynamically bound, but default parameter values are statically bound. An object’s static type is the type you declare it to have
阅读全文
摘要:Consider: class B { public: void mf(); // ... }; class D : public B { /*...*/ }; D x; // x is an object of type D B *pB = &x; // get pointer to x pB->
阅读全文
摘要:The Template Method Pattern via the Non-Virtual Interface Idiom: class GameCharacter { public: int healthValue() const // derived classes do not redef
阅读全文
摘要:The seemingly straightforward notion of (public) inheritance turns out, upon closer examination, to be composed of two separable parts: inheritance of
阅读全文
摘要:C++'s name-hiding rules do just that: hide names. Whether the names correspond to the same or different types is immaterial. int x; // global variable
阅读全文
摘要:Consider this code: class Rectangle { public: virtual void setHeight(int newHeight); virtual void setWidth(int newWidth); virtual int height() const;
阅读全文
摘要:C++ doesn't do a very good job of separating interfaces from implementations. A class definition specifies not only a class interface but also a fair
阅读全文
摘要:When you inline a function, you may enable compilers to perform context-specific optimizations on the body of the function. Most compilers never perfo
阅读全文
摘要:Suppose we have a class for representing GUI menus with background images. The class is designed to be used in a threaded environment, so it has a mut
阅读全文
摘要:Suppose you’re working on an application involving rectangles. Each rectangle can be represented by its upper left corner and its lower right corner.
阅读全文
摘要:Let’s begin with a review of casting syntax, because there are usually three different ways to write the same cast. C-style casts look like this: (T)
阅读全文
摘要:This suggests the real meaning of "as long as possible" in this Item’s title. Not only should you postpone a variable's definition until right before
阅读全文
摘要:To swap the values of two objects is to give each the other's value. By default, swapping is accomplished via the standard swap algorithm. Its typical
阅读全文
摘要:You might start your Rational class this way: class Rational { public: Rational(int numerator = 0, // ctor is deliberately not explicit; int denominat
阅读全文
摘要:Imagine a class for representing web browsers. Among the many functions such a class might offer are those to clear the cache of downloaded elements,
阅读全文
摘要:If everything in the public interface is a function, clients won't have to scratch their heads trying to remember whether to use parentheses when they
阅读全文
摘要:Consider a class for representing rational numbers, including a function for multiplying two rationals together: class Rational { public: Rational(int
阅读全文
摘要:By default, C++ passes objects to and from functions by value (a characteristic it inherits from C).The end result is that passing an object by value
阅读全文
摘要:How do you design effective classes? First, you must understand the issues you face. Virtually every class requires that you confront the following qu
阅读全文
摘要:Developing interfaces that are easy to use correctly and hard to use incorrectly requires that you consider the kinds of mistakes that clients might m
阅读全文
摘要:Suppose we have a function to reveal our processing priority and a second function to do some processing on a dynamically allocated Widget in accord w
阅读全文
摘要:When you employ a new expression (i.e., dynamic creation of an object via a use of new), two things happen. First, memory is allocated (via a function
阅读全文
摘要:,Item 13 introduces the idea of using smart pointers like auto_ptr or tr1::shared_ptr to hold the result of a call to a factory function like createIn
阅读全文
摘要:For example, suppose you’re using a C API to manipulate mutex objects of type Mutex offering functions lock and unlock: void lock(Mutex *pm); // lock
阅读全文
摘要:Consider this code: class Investment { /*...*/ }; // root class of hierarchy of investment types Investment* createInvestment(); // return ptr to dyna
阅读全文

浙公网安备 33010602011771号