このリファレンスによるとoperator new
:
グローバル動的ストレージ演算子関数は、標準ライブラリでは特別です:
- operator new の 3 つのバージョンはすべて、std 名前空間ではなく、グローバル名前空間で宣言されています。
- 最初と 2 番目のバージョンは、C++ プログラムのすべての翻訳単位で暗黙的に宣言されます。ヘッダーを含める必要はありません。
operator new
これは、 (placement )の 3 番目のバージョンがnew
C++ プログラムのすべての翻訳単位で暗黙的に宣言されているわけではなく、存在するためにヘッダー<new>
を含める必要があることを暗示しているように思えます。あれは正しいですか?
もしそうなら、g++ と MS VC++ Express の両方のコンパイラを使用しているのに、ソース コードに含まれていnew
ない3 番目のバージョンを使用してコードをコンパイルできるように見えるのはなぜですか?#include <new>
また、MSDN 標準 C++ ライブラリ リファレンス エントリには、ステートメントを含むoperator new
3 つの形式のサンプル コードがいくつか示されていますが、この例は、このインクルードがなくても同じようにコンパイルおよび実行されるようです。operator new
#include <new>
// new_op_new.cpp
// compile with: /EHsc
#include<new>
#include<iostream>
using namespace std;
class MyClass
{
public:
MyClass( )
{
cout << "Construction MyClass." << this << endl;
};
~MyClass( )
{
imember = 0; cout << "Destructing MyClass." << this << endl;
};
int imember;
};
int main( )
{
// The first form of new delete
MyClass* fPtr = new MyClass;
delete fPtr;
// The second form of new delete
char x[sizeof( MyClass )];
MyClass* fPtr2 = new( &x[0] ) MyClass;
fPtr2 -> ~MyClass();
cout << "The address of x[0] is : " << ( void* )&x[0] << endl;
// The third form of new delete
MyClass* fPtr3 = new( nothrow ) MyClass;
delete fPtr3;
}
誰かがこれに光を当てることができますか?いつ、なぜ必要な#include <new>
の#include <new>
ですか?