在 C++ 中,如何在没有动态分配(即 NEW)的情况下将对象初始化为对象内部的指针?

发布于 2024-12-27 02:41:34 字数 159 浏览 0 评论 0 原文

例如:

class A{

int b;
A *a1;
A *a2;

A(int c):b(c), a1(c), a2(c)  {}

}

我认为这就是方法,但它无法编译。 有没有办法做到这一点,或者是否有必要始终使用动态分配? 谢谢

For Example:

class A{

int b;
A *a1;
A *a2;

A(int c):b(c), a1(c), a2(c)  {}

}

I thought this was the way, but it doesn't compile.
Is there a way to do this, or is it necessary to always use dynamic allocation?
thanks

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

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

发布评论

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

评论(3

一枫情书 2025-01-03 02:41:34

您可以简单地将指针初始化为 null。

class A{

   int b;
   A *a1;
   A *a2;

   A(int c):b(c), a1(NULL), a2(NULL)  {}

}

(请注意,如果它抱怨未定义 NULL,#include >)

或者根本不初始化它们。

class A{

   int b;
   A *a1;
   A *a2;

   A(int c):b(c)  {}

}

You could simply initialize the pointers to null.

class A{

   int b;
   A *a1;
   A *a2;

   A(int c):b(c), a1(NULL), a2(NULL)  {}

}

(Note that if it complains about NULL not being defined, #include <cstddef>)

Or not initialize them at all.

class A{

   int b;
   A *a1;
   A *a2;

   A(int c):b(c)  {}

}
风苍溪 2025-01-03 02:41:34

目前还不清楚你想做什么,但你当然可以做到:

class A{

int b;
A *a1;
A *a2;

A(int c, A* pa1, A* pa2):b(c), a1(pa1), a2(pa2)  {}

}

你可以这样称呼:

A m1(1, NULL, NULL);
A m2(2, NULL, NULL);
if(something())
{
  A m3(3, &m1, &m2);
  // do stuff with m3
}

小心点。如果 m1m2m3 之前超出范围,则 m3 将保留指向随机的指针垃圾。

It's not clear what you're trying to do, but you certainly can do it:

class A{

int b;
A *a1;
A *a2;

A(int c, A* pa1, A* pa2):b(c), a1(pa1), a2(pa2)  {}

}

Which you can call like this:

A m1(1, NULL, NULL);
A m2(2, NULL, NULL);
if(something())
{
  A m3(3, &m1, &m2);
  // do stuff with m3
}

Just be careful. If m1 or m2 go out of scope before m3, then m3 will be left with pointers that point to random garbage.

黯然#的苍凉 2025-01-03 02:41:34

如果您不想像这样初始化指向 nullptrNULL0 pre-C++11)的指针:

class A {
    A(int c) : b(c), a1(), b1() { }

    int b;
    A* a1, *a2;
};

那么指针指向的对象必须存在于某个外部作用域中,这样它的生存时间至少与包含指针的对象一样长,否则您必须使用 new。

您可以接受 A* 将指针设置为:(

class A {
    A(int c, A* aptr) : b(c), a1(aptr), a2(aptr) { }

    ... 
};

您不能为此使用引用,因为这将使 A 成为 A 的要求,这是一个不可能满足的条件。不过,您可以创建一个接受引用的附加构造函数。)

在您的示例中,您尝试使用 int 来初始化一个指针,但它不会工作。另外,如果您在示例中创建一个对象(通过 new 或其他方式),您将耗尽内存,因为您分配的每个 A 都会再分配两个 A 并且该循环将继续,直到内存耗尽。

If you don't want to initialise the pointers to nullptr (NULL or 0 pre-C++11) like this:

class A {
    A(int c) : b(c), a1(), b1() { }

    int b;
    A* a1, *a2;
};

then either the object you make the pointer point to has to exist in some outer scope so that it will live at least as long as the object that contains the pointer, or you have to use new.

You could accept an A* to set the pointers to:

class A {
    A(int c, A* aptr) : b(c), a1(aptr), a2(aptr) { }

    ... 
};

(You can't use a reference for this because that would make an A a requirement for an A, an impossible condition to satisfy. You could make an additional constructor that accepted a reference though.)

In your example, you are trying to use an int to initialise a pointer which won't work. Also, if you create an object in your example (via new or whatever), you will run out of memory because each A you allocate will allocate two more As and that cycle will continue until you run out of memory.

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