摘要:The Abstract Factory and Factory Method patterns are very similar in many ways. It’s very confusing tomany people when to use which one of them. Both of them can be used for the same purposes of creatingobjects without letting clients know what exact concrete objects are being returned. The followi.
阅读全文
摘要:The abstract Product defines the interface of objects the factory method creates. TheConcreteProduct implements the Product interface. The Creator defines the factorymethod that returns an object of Product. It may also define a default implementation ofthe factory method that returns a default Con.
阅读全文
摘要:Familiarize yourself with the language standard.
阅读全文
摘要:Make sure the C++ and C compilers produce compatible object files.Declare functions to be used by both languages extern "C".If at all possible, write main in C++.Always use delete with memory from new; always use free with memory from malloc.Limit what you pass between the two languages to
阅读全文
摘要:The general rule remains: non-leaf classes should be abstract. You may need to bend the rule when working with outside libraries, but in code over which you have control, adherence to it will yield dividends in the form of increased reliability, robustness, comprehensibility, and extensibility t...
阅读全文
摘要:The Prototype pattern is one of the simplest design patterns. A client knows an abstractPrototype class. At runtime any object that is a subclass of the abstract Prototype canbe cloned at the client’s will. So the client can make multiple instances of the same typewithout creating them manually.
阅读全文
摘要:Good software adapts well to change. It accommodates new features, it ports to new platforms, it adjusts to new demands, it handles new inputs. Software this flexible, this robust, and this reliable does not come about by accident. It is designed and implemented by programmers who conform to th...
阅读全文
摘要:Let us assume, however, that you must implement your game in C++ — that you must come up with your own way of implementing what is commonly referred to as double-dispatching. (The name comes from the object-oriented programming community, where what C++ programmers know as a virtual function ca...
阅读全文
摘要:Proxy classes allow you to achieve some types of behavior that are otherwise difficult or impossible to implement. Multidimensional arrays are one example, lvalue/rvalue differentiation is a second, suppression of implicit conversions (see Item 5) is a third. At the same time, proxy classes hav...
阅读全文
摘要:When you use smart pointers in place of C++'s built-in pointers (i.e., dumb pointers), you gain control over the following aspects of pointer behavior:Construction and destruction. You determine what happens when a smart pointer is created and destroyed. It is common to give smart pointers a def
阅读全文
摘要:The idea is to take advantage of the fact that when an object is allocated on the heap, operator new is called to allocate the raw memory, then a constructor is called to initialize an object in that memory. Preventing clients from directly instantiating objects on the heap is easy, because such...
阅读全文
摘要:Now consider what inline means. Conceptually, it means compilers should replace each call to the function with a copy of the function body, but for non-member functions, it also means something else. It means the functions in question have internal linkage. You don't ordinarily need to worry ...
阅读全文
摘要:Consider what readComponent does. It creates a new object, either a TextBlock or a Graphic, depending on the data it reads. Because it creates new objects, it acts much like a constructor, but because it can create different types of objects, we call it a virtual constructor. A virtual construc...
阅读全文
摘要:When a virtual function is called, the code executed must correspond to the dynamic type of the object on which the function is invoked; the type of the pointer or reference to the object is immaterial. How can compilers provide this behavior efficiently? Most implementations use virtual tables...
阅读全文
摘要:The contrast in performance between iostreams and stdio is just an example, however, it's not the main point. The main point is that different libraries offering similar functionality often feature different performance trade-offs, so once you've identified the bottlenecks in your software (
阅读全文
摘要:All this talk of named objects, unnamed objects, and compiler optimizations is interesting, but let us not forget the big picture. The big picture is that assignment versions of operators (such as operator+=) tend to be more efficient than stand-alone versions of those operators (e.g. operator+...
阅读全文
摘要:Overloading to avoid temporaries isn't limited to operator functions. For example, in most programs, you'll want to allow a string object everywhere a char* is acceptable, and vice versa. Similarly, if you're using a numerical class like complex (see Item 35), you'll want types like
阅读全文
摘要:The rules for C++ allow compilers to optimize temporary objects out of existence. As a result, if you call operator* in a context like this,Rational a = 10;Rational b(1, 2);Rational c = a * b; // operator* is called here your compilers are allowed to eliminate both the ...
阅读全文
摘要:The bottom line is that temporary objects can be costly, so you want to eliminate them whenever you can. More important than this, however, is to train yourself to look for places where temporary objects may be created. Anytime you see a reference-to-const parameter, the possibility exists that ...
阅读全文
摘要:There is a common theme running through this Item, and that's that greater speed can often be purchased at a cost of increased memory usage. Keeping track of running minima, maxima, and averages requires extra space, but it saves time. Caching results necessitates greater memory usage but reduce
阅读全文
摘要:There's nothing about lazy evaluation that's specific to C++. The technique can be applied in any programming language, and several languages — notably APL, some dialects of Lisp, and virtually all dataflow languages — embrace the idea as a fundamental part of the language. Mainstream progra
阅读全文