これは簡単な質問です:
new 演算子を使用すると、型 (void *) のポインターが返されますか? new/delete と malloc/free の違いは何ですか? を参照してください。答え - それは言うnew returns a fully typed pointer while malloc void *
しかし、http: //www.cplusplus.com/reference/new/operator%20new/によると
throwing (1)
void* operator new (std::size_t size) throw (std::bad_alloc);
nothrow (2)
void* operator new (std::size_t size, const std::nothrow_t& nothrow_value) throw();
placement (3)
void* operator new (std::size_t size, void* ptr) throw();
つまり、(void *) 型のポインターを返します。(void *) を返す場合、MyClass *ptr = (MyClass *)new MyClass; のようなコードは見たことがありません。
私は混乱しています。
編集
http://www.cplusplus.com/reference/new/operator%20new/の 例によると
std::cout << "1: ";
MyClass * p1 = new MyClass;
// allocates memory by calling: operator new (sizeof(MyClass))
// and then constructs an object at the newly allocated space
std::cout << "2: ";
MyClass * p2 = new (std::nothrow) MyClass;
// allocates memory by calling: operator new (sizeof(MyClass),std::nothrow)
// and then constructs an object at the newly allocated space
したがって、MyClass * p1 = new MyClass
構文を正しく理解していればoperator new (sizeof(MyClass))
、 andが返されるはずです。throwing (1)
void* operator new (std::size_t size) throw (std::bad_alloc);(void *)
ありがとう