演算子のオーバーロードが新しく削除されるのを少し混乱させています。 私はいくつかのテストを書いた:C++ - オーバーロード演算子new
#include <iostream>
using namespace std;
class Test
{
public :
Test()
{
cout << "Test - ctor" <<endl;
}
~Test()
{
cout << "Test - dtor" <<endl;
}
Test(const Test& t)
{
cout << "Test - copy ctor" <<endl;
}
Test& operator = (const Test& t)
{
cout << "Test - assiment operator" <<endl;
}
void* operator new(size_t size)
{
cout << "Test - operator new" <<endl;
return NULL;
}
void print()
{
cout << "print" << endl;
}
};
int main()
{
Test* t = new Test();
t->print();
return 0;
}
、出力は次のとおりです。今
Test - operator new
Test - ctor
print
私は「新しい」から「NULL」を返す場合、私は印刷に呼び出すときに、なぜ私のプログラムがクラッシュしません関数?ありがとう。
が、それは定期的に未定義の動作です。 'Test * t = nullptr;を試してください。 t-> print(); 'と何が起こるかを見てください。 – molbdnilo
UBはUBと同様です。 – George