相关疑难解决方法(0)

C++中有新的返回(void*)吗?

这是一个简单的问题:

使用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();
Run Code Online (Sandbox Code Playgroud)

这意味着它返回一个类型为(void*)的指针,如果它返回(void*)我从未见过像MyClass*ptr =(MyClass*)new MyClass这样的代码;

我很困惑.

编辑

根据http://www.cplusplus.com/reference/new/operator%20new/ example

std::cout << "1: ";
  MyClass * p1 = new MyClass;
      // allocates memory by calling: operator new (sizeof(MyClass))
      // and then constructs an object …
Run Code Online (Sandbox Code Playgroud)

c++

6
推荐指数
1
解决办法
2265
查看次数

标签 统计

c++ ×1