在C++中分配一般类为null

发布于 2025-01-30 16:35:25 字数 188 浏览 2 评论 0原文

我在调整程序从C#到C ++时遇到了麻烦。给定课程将其分配为空值是非常令人讨厌的。但是C ++不接受等效形式“ nullptr”,

class Point{
     public:
         int x,y;
}
//...
Point p = nullptr;

有某种方法可以解决它吗?

I am having kind of a trouble adapting a program from C# to C++. It's very commom given a class to assign it the null value. But C++ is not accepting the equivalent form 'nullptr'

class Point{
     public:
         int x,y;
}
//...
Point p = nullptr;

There is some way of solving it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏辞 2025-02-06 16:35:25

您不能将nullptr分配给类,因为类是一种类型而不是变量。通过键入点P = nullptr;您正在做什么,因为您要编译器找到point接受nullptr >。

您可以通过执行此操作来创建point作为自动变量的对象:

Point p = Point{1, 2}; // sets x = 2 and y = 3

或者简单地:

Point p;

为此,您需要定义一个初始化x和<的构造函数代码> y ,因为您尚未为成员变量提供默认值。

根据该语句居住的上下文,p将分配在堆或堆栈上。如果您在主函数中实例化p,则将是后者。

您可以通过执行此操作来创建指向Point对象的指针:

Point* p = nullptr;

但是,您只会创建一个指针。您将需要进一步照顾分配,您可以这样做:

Point* p = new Point{1, 2};

在这种情况下,您还应在程序结束之前释放分配的内存:

delete p;

You cannot assign nullptr to a class because a class is a type and not a variable. What you're doing by typing Point p = nullptr; is that you're asking the compiler to find a constructor of Point which accepts a nullptr.

You can either create an object of Point as an automatic variable by doing this:

Point p = Point{1, 2}; // sets x = 2 and y = 3

or simply like this:

Point p;

for which however you will need to define a constructor which initializes x and y because you haven't provided the member variables with default values.

Depending on the context in which this statement resides, p will either be allocated on the heap or on the stack. In case you're instantiating p in the main function, it will be the latter.

You can create a pointer to an object of Point by doing this:

Point* p = nullptr;

However, you will only have created a pointer. You will need to further take care of allocation, which you can do like this:

Point* p = new Point{1, 2};

in which case you should also free the allocated memory before your program ends like so:

delete p;
迷途知返 2025-02-06 16:35:25

问题p是您示例中的一种非分子类型。这意味着它不能用nullptr初始化。 p的类型是

您可以制作p一个指向point的指针,以便可以使用nullptr初始化它,如下所示:

Point *p = nullptr; //p is a pointer to non-const Point object

或者

如果要创建一个类型的对象point然后这样做的一种方法是:

Point p{1, 3};

我还建议参考a 良好的C ++书为了更好地理解该概念。

The problem is that p is a non-pointer type in your example. This means that it cannot be initialized with a nullptr. The type of p is Point.

You can make p a pointer to a Point so that you can initialize it with a nullptr as shown below:

Point *p = nullptr; //p is a pointer to non-const Point object

Or

If you want to create an object of the class type Point then one way of doing so is as follows:

Point p{1, 3};

I would also recommend referring to a good C++ book for a better understanding of the concept.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文