⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ei34.htm

📁 一个非常适合初学者入门的有关c++的文档
💻 HTM
📖 第 1 页 / 共 2 页
字号:
 Effective C++, 2E | Item 34: Minimize compilation dependencies between files Back to Item 33: Use inlining judiciously.Continue to Inheritance and Object-Oriented DesignItem 34: Minimize compilation dependencies between files.So you go into your C++ program and you make a minor change to the implementation of a class. Not the class interface, mind you, just the implementation; only the private stuff. Then you get set to rebuild the program, figuring that the compilation and linking should take only a few seconds. After all, only one class has been modified. You click on Rebuild or type make (or its moral equivalent), and you are astonished, then mortified, as you realize that the whole world is being recompiled and relinked!Don't you just hate it when that happens?The problem is that C++ doesn't do a very good job of separating interfaces from implementations. In particular, class definitions include not only the interface specification, but also a fair number of implementation details. For example: class Person {public:  Person(const string& name, const Date& birthday,         const Address& addr, const Country& country);  virtual ~Person();  ...                      // copy constructor and assignment                           // operator omitted for simplicity  string name() const;  string birthDate() const;  string address() const;  string nationality() const;private:  string name_;            // implementation detail  Date birthDate_;         // implementation detail  Address address_;        // implementation detail  Country citizenship_;    // implementation detail};This is hardly a Nobel Prize-winning class design, although it does illustrate an interesting naming convention for distinguishing private data from public functions when the same name makes sense for both: the former are tagged with a trailing underbar. The important thing to observe is that class Person can't be compiled unless the compiler also has access to definitions for the classes in terms of which Person is implemented, namely, string, Date, Address, and Country. Such definitions are typically provided through #include directives, so at the top of the file defining the Person class, you are likely to find something like this: #include <string>           // for type string (see Item 49)#include "date.h"#include "address.h"#include "country.h"Unfortunately, this sets up a compilation dependency between the file defining Person and these include files. As a result, if any of these auxiliary classes changes its implementation, or if any of the classes on which it depends changes its implementation, the file containing the Person class must be recompiled, as must any files that use the Person class. For clients of Person, this can be more than annoying. It can be downright incapacitating.You might wonder why C++ insists on putting the implementation details of a class in the class definition. For example, why can't you define Person this way, class string;         // "conceptual" forward declaration for the                      // string type. See Item 49 for details.class Date;           // forward declarationclass Address;        // forward declarationclass Country;        // forward declarationclass Person {public:  Person(const string& name, const Date& birthday,         const Address& addr, const Country& country);  virtual ~Person();  ...                      // copy ctor, operator=  string name() const;  string birthDate() const;  string address() const;  string nationality() const;};specifying the implementation details of the class separately? If that were possible, clients of Person would have to recompile only if the interface to the class changed. Because interfaces tend to stabilize before implementations do, such a separation of interface from implementation could save untold hours of recompilation and linking over the course of a large software effort.Alas, the real world intrudes on this idyllic scenario, as you will appreciate when you consider something like this: int main(){  int x;                      // define an int  Person p(...);              // define a Person                              // (arguments omitted for  ...                         // simplicity)}When compilers see the definition for x, they know they must allocate enough space to hold an int. No problem. Each compiler knows how big an int is. When compilers see the definition for p, however, they know they have to allocate enough space for a Person, but how are they supposed to know how big a Person object is? The only way they can get that information is to consult the class definition, but if it were legal for a class definition to omit the implementation details, how would compilers know how much space to allocate?In principle, this is no insuperable problem. Languages such as Smalltalk, Eiffel, and Java get around it all the time. The way they do it is by allocating only enough space for a pointer to an object when an object is defined. That is, they handle the code above as if it had been written like this: int main(){  int x;                     // define an int  Person *p;                 // define a pointer                             // to a Person  ...}It may have occurred to you that this is in fact legal C++, and it turns out that you can play the "hide the object implementation behind a pointer" game yourself.Here's how you employ the technique to decouple Person's interface from its implementation. First, you put only the following in the header file declaring the Person class: // compilers still need to know about these type// names for the Person constructorclass string;      // again, see Item 49 for information                   // on why this isn't correct for stringclass Date;class Address;class Country;// class PersonImpl will contain the implementation// details of a Person object; this is just a// forward declaration of the class nameclass PersonImpl;class Person {public:  Person(const string& name, const Date& birthday,         const Address& addr, const Country& country);  virtual ~Person();  ...                               // copy ctor, operator=  string name() const;  string birthDate() const;  string address() const;  string nationality() const;private:  PersonImpl *impl;                 // pointer to implementation};Now clients of Person are completely divorced from the details of strings, dates, addresses, countries, and persons. Those classes can be modified at will, but Person clients may remain blissfully unaware. More to the point, they may remain blissfully un-recompiled. In addition, because they're unable to see the details of Person's implementation, clients are unlikely to write code that somehow depends on those details. This is a true separation of interface and implementation.The key to this separation is replacement of dependencies on class definitions with dependencies on class declarations. That's all you need to know about minimizing compilation dependencies: make your header files self-sufficient whenever it's practical, and when it's not practical, be dependent on class declarations, not class definitions. Everything else flows from this simple design strategy.There are three immediate implications: Avoid using objects when object references and pointers will do. You may define references and pointers to a type with only a declaration for the type. Defining objects of a type necessitates the presence of the type's definition. Use class declarations instead of class definitions whenever you can. Note that you never need a class definition to declare a function using that class, not even if the function passes or returns the class type by value:   class Date;                    // class declaration  Date returnADate();            // fine  no definition  void takeADate(Date d);        // of Date is neededOf course, pass-by-value is generally a bad idea (see Item 22), but if you find yourself forced to use it for some reason, there's still no justification for introducing unnecessary compilation dependencies.If you're surprised that the declarations for returnADate and takeADate compile without a definition for Date, join the club; so was I. It's not as curious as it seems, however, because if anybody calls those functions, Date's definition must be visible. Oh, I know what you're thinking: why bother to declare functions that nobody calls? Simple. It's not that nobody calls them, it's that not everybody calls them. For example, if you have a library containing hundreds of function declarations (possibly spread over several namespaces see Item 28), it's unlikely that every client calls every function. By moving the onus of providing class definitions (via #include directives) from your header file of function declarations to clients' files containing function calls, you eliminate artificial client dependencies on type definitions they don't really need. Don't #include header files in your header files unless your headers won't compile without them. Instead, manually declare the classes you need, and let clients of your header files #include the additional headers necessary to make their code compile. A few clients may grumble that this is inconvenient, but rest assured that you are saving them much more pain than you're inflicting. In fact, this technique is so well-regarded, it's enshrined in the standard C++ library (see Item 49); the header <iosfwd> contains declarations (and only declarations) for the types in the iostream library.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -