';新';操作员理解 需要详细解释C++中的“新”运算符。 有人能解释一下为什么以下方法有效: int *n = new int(10); // initialize the pointer to integer as '10' cout << *n; // o/p -> 10
与指针一起使用的新运算符,而不是int类型 所以 您可以将它与类struct等一起使用,但不能与纯类型一起使用 我是说只是int字符。。等等 这是新的宣言:';新';操作员理解 需要详细解释C++中的“新”运算符。 有人能解释一下为什么以下方法有效: int *n = new int(10); // initialize the pointer to integer as '10' cout << *n; // o/p -> 10,c++,C++,与指针一起使用的新运算符,而不是int类型 所以 您可以将它与类struct等一起使用,但不能与纯类型一起使用 我是说只是int字符。。等等 这是新的宣言: void* operator new[] (std::size_t size); void* operator new[] (std::size_t size, const std::nothrow_t& nothrow_value) noexcept; void* operator new[] (std::size_t
void* operator new[] (std::size_t size);
void* operator new[] (std::size_t size, const std::nothrow_t& nothrow_value) noexcept;
void* operator new[] (std::size_t size, void* ptr) noexcept;
正如您所看到的,它是返回指针类型void,所以您不能在c/c中使用它++
写
这与您所做的相同。从错误消息本身可以明显看出。。 错误:从“int*”到“int”的转换无效。
因为如果“new”成功,它将返回一个指针。这就是为什么当您试图将其分配给int时会出现错误。为什么您希望第二个可以工作<代码>新建返回指针。是否重复?可能重复,那么
int*n
和int p
之间有什么区别?
int x = 5;
int *p = &x;
int *q = new int(4);
void* operator new[] (std::size_t size);
void* operator new[] (std::size_t size, const std::nothrow_t& nothrow_value) noexcept;
void* operator new[] (std::size_t size, void* ptr) noexcept;
int x = p; // error : invalid conversion from 'int*' to 'int'