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

📄 ei9.htm

📁 一个非常适合初学者入门的有关c++的文档
💻 HTM
字号:
 Effective C++, 2E | Item 9: Avoid hiding the "normal" form of new. Back to Item 8: Adhere to convention when writing operator new and operator delete.Continue to Item 10: Write operator delete if you write operator new.Item 9: Avoid hiding the "normal" form of new.A declaration of a name in an inner scope hides the same name in outer scopes, so for a function f at both global and class scope, the member function will hide the global function: void f();                             // global functionclass X {public:  void f();                           // member function};X x;f();                                  // calls global fx.f();                                // calls X::fThis is unsurprising and normally causes no confusion, because global and member functions are usually invoked using different syntactic forms. However, if you add to this class an operator new taking additional parameters, the result is likely to be an eye-opener: class X {public:  void f();  // operator new allowing specification of a  // new-handling function  static void * operator new(size_t size, new_handler p);};void specialErrorHandler();          // definition is elsewhereX *px1 =  new (specialErrorHandler) X;       // calls X::operator newX *px2 = new X;                      // error!By declaring a function called "operator new" inside the class, you inadvertently block access to the "normal" form of new. Why this is so is discussed in Item 50. Here we're more interested in figuring out how to avoid the problem.One solution is to write a class-specific operator new that supports the "normal" invocation form. If it does the same thing as the global version, that can be efficiently and elegantly encapsulated as an inline function: class X {public:  void f();  static void * operator new(size_t size, new_handler p);  static void * operator new(size_t size)  { return ::operator new(size); }};X *px1 =  new (specialErrorHandler) X;      // calls X::operator                                    // new(size_t, new_handler)X* px2 = new X;                     // calls X::operator                                    // new(size_t)An alternative is to provide a default parameter value (see Item 24) for each additional parameter you add to operator new: class X {public:  void f();  static    void * operator new(size_t size,                // note default                        new_handler p = 0);         // value for p};X *px1 = new (specialErrorHandler) X;               // fineX* px2 = new X;                                     // also fineEither way, if you later decide to customize the behavior of the "normal" form of new, all you need to do is rewrite the function; callers will get the customized behavior automatically when they relink. Back to Item 8: Adhere to convention when writing operator new and operator delete.Continue to Item 10: Write operator delete if you write operator new. 

⌨️ 快捷键说明

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