Questa è una semplice domanda:Il nuovo ritorno (void *) in C++?
Il nuovo operatore utilizza un puntatore di tipo (void *)? riferimento alla What is the difference between new/delete and malloc/free? risposta - dice new returns a fully typed pointer while malloc void *
Ma secondo 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();
che significa che restituisce un puntatore di tipo (void *), se restituisce (void *) Non ho mai visto un codice come MyClass * ptr = (MyClass *) new MyClass;
Mi sono confuso.
EDIT
Come per esempio 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
Così MyClass * p1 = new MyClass
chiamate operator new (sizeof(MyClass))
e dal momento throwing (1)
deve restituire
void* operator new (std::size_t size) throw (std::bad_alloc);(void *)
se ho capito bene la sintassi.
Grazie
[Plug Shameless] (http://stackoverflow.com/a/8962536/775806) –
[voce cppreference per new-expression] (http://en.cppreference.com/w/cpp/language/new) – dyp
@DyP Ok .. capito ... cosa intendevi dire? La nuova espressione (new int) usa una funzione di allocazione (operatore nuovo). La funzione di allocazione fornisce solo lo storage, new-expression new type-id restituisce un puntatore a type-id (o throw) '.. Grazie allo standard –