为什么第 18 行 Node* bob = new();
没有给我错误,而第 22 行 node actualValue = new Node;
有。
#include <iostream>
using namespace std;
class Node{
Node* node;
public:
int data;
};
int main() {
cout << "Hello, World!" << std::endl;
int a = 200;
int* ptr = &a;
cout << a << " pointer is " << ptr << endl;
Node* bob = new Node();
bob->data = 1;
Node actualValue = new Node;
return 0;
}
最佳答案
Node* bob = new Node();
不要报错,因为 new Node()
返回一个指针 Node*
并且bob
的类型也是 Node*
。您可以在具有相同类型的指针之间进行赋值。
Node actualValue = new Node;
给出错误,因为 actualValue
的类型是 Node
和类 Node
没有任何接受指针 Node*
的构造函数。
关于c++ - 我是 c++ 的新手,你能解释一下这个例子中的指针吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62797492/