我可以在堆上创建一个新结构而不定义构造函数吗?

发布于 2024-10-07 10:35:52 字数 445 浏览 6 评论 0原文

我知道 C++ 中的结构和类之间几乎没有什么区别(两个?)。尽管如此,我被指示使用结构来定义简单的小东西,例如可能不需要成员函数的节点(尽管事实上我在技术上可以包含成员函数)。例如,我可以将节点定义为链表类的私有成员,如下所示:

class LinkedList {
  struct Node {
    MyObject *data;
    Node *next;
  };

  Node *list;

};

但是,在这种情况下,是否可以在堆上创建此结构的新实例,或者我需要定义一个构造函数吗?有没有办法在没有 new 运算符的情况下在堆上创建东西?或者,更好的是:我是否没有必要如此严格地坚持不应该为结构定义成员函数的观念?我应该继续定义一个吗?或者如果我这样做,是否就像承认 Node 确实应该是一个内部类,而不是一个内部结构?我真的应该担心这些事情吗?哪个更具可读性?

谢谢!

I understand that there are very few differences between structs and classes in c++ (two?). Be that as it may, I've been instructed to use structs to define simple little things like nodes that might not need member functions (despite the fact that I could technically include include member functions). For instance I might define a node as a private member of a linked list class as follows:

class LinkedList {
  struct Node {
    MyObject *data;
    Node *next;
  };

  Node *list;

};

In this case, however, is it possible to create a new instance of this struct on the heap, or would I need to define a constructor? Is there a way to create things on the heap without the new operator? Or, better yet: is it unnecessary for me to cling so tightly to the notion that I shouldn't define member functions for structs? Should I just go ahead and define one? Or if I did that, would it be like admitting that Node really aught to be an inner class, rather than an inner struct? Should I really be worrying about these sorts of things at all? Which is more readable?

Thanks!

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

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

发布评论

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

评论(5

滥情哥ㄟ 2024-10-14 10:35:53

我真的应该担心这些事情吗?

是的。正如您所说,结构和类之间确实没有重要的区别。如果您被告知要创建一个struct,并且它至少需要一个成员函数,那么定义一个成员函数

这确实没什么大不了的。

但除此之外,您当然可以在没有构造函数的情况下堆分配结构(否则 malloc 在 C 中将毫无意义。该语言甚至没有构造函数

struct S,您可以简单地调用new Snew S(),具体取决于您想要哪种初始化。如果您没有定义构造函数,编译器会为您创建一个。

Should I really be worrying about these sorts of things at all?

Yes. As you say, there really are no important differences between structs and classes. If you've been told to make a struct, and it needs at least one member function, then define a member function.

It's really no big deal.

But apart from that, you can of course heap-allocate structs without constructors (otherwise malloc would be rather pointless in C. That language doesn't even have constructors)

Given a struct S, you can simply call new S or new S(), depending on what kind of initialization you want. if you don't define a constructor, the compiler makes one for you.

随心而道 2024-10-14 10:35:52

但是,在这种情况下,是否可以在堆上创建此结构的新实例,或者我需要定义一个构造函数吗?有没有办法在没有 new 运算符的情况下在堆上创建东西?

正如之前所回答的,您可以通过 new 或使用 malloc 在堆上创建一个新实例。

或者,更好的是:我是否没有必要如此严格地坚持不应该为结构定义成员函数的观念?

这是更有趣的问题。 C++ 中的 struct 和 class 之间的主要(唯一?)区别是
默认访问说明符。也就是说,struct 默认为公共访问,class 默认为私有。在我看来,这就是决定您使用两者中哪一个的区别。基本上,如果用户应该直接访问成员,那么它应该是一个struct

例如,如果您没有成员函数,那么显然其目的是直接访问对象的成员,因此它将是一个struct。如果对象只是用于实现其外部类的一个小的 private 帮助器,如您的示例所示,那么即使它具有成员函数,通常也最清楚地允许外部类访问到它的成员,所以它应该是一个struct。通常,对于这些类,外部类的实现与内部类的实现紧密耦合,因此没有理由隐藏其中一个类。

因此,对于简单的(例如 std::pair)对象或那些使用受到限制的对象(如在私有内部类中),对成员的默认访问可能是一件好事,在这些情况下,我会将它们设为结构体代码>.

In this case, however, is it possible to create a new instance of this struct on the heap, or would I need to define a constructor? Is there a way to create things on the heap without the new operator?

As answered before, you can create a new instance on the heap either via new or with malloc.

Or, better yet: is it unnecessary for me to cling so tightly to the notion that I shouldn't define member functions for structs?

This is the more interesting question. The major (only?) difference between struct and class in c++ is the
default access specifier. That is, struct defaults to public access and class defaults to private. In my opinion, this is the difference that should determine which of the two you use. Basically, if users should access the members directly, then it should be a struct.

If, for example, you have no member functions, then obviously the intention is for the object's members to be accessed directly and so it would be a struct. In the case of an object that is just a small private helper for the implementation of its outer class, as in your example, then even if it has member functions it is often clearest to allow the outer class access to its members and so it should be a struct. Often with these classes the implementation of the outer class is tightly coupled to the implementation of the inner class and so there is no reason to hide the one from the other.

So, for trivial (e.g. std::pair) objects or those whose use is limited (as in a private inner class) default access to members can be a good thing, and in these cases I would make them structs.

兔姬 2024-10-14 10:35:52

即使您没有定义构造函数,编译器也会创建一个默认构造函数,因此您可以使用运算符“new”:

Node *n = new Node;

AFAIAC,结构是一个类,只是其“公共性”默认值相反。

Even if you don't define a constructor, the compiler will create a default one and so you can use operator 'new':

Node *n = new Node;

AFAIAC, a struct is a class, except that its "publicness" default is reversed.

说好的呢 2024-10-14 10:35:52

Malloc 工作正常:

Node *n = (Node*)malloc(sizeof(*n));

只需记住 free() 任何 malloc()'d 和 delete 任何 new'd 。

Malloc works fine:

Node *n = (Node*)malloc(sizeof(*n));

Just remember to free() anything malloc()'d and delete anything new'd.

夏雨凉 2024-10-14 10:35:52

但是,在这种情况下,是否可以在
堆,或者我需要定义一个构造函数吗?

是的,您可以在堆上创建它而无需定义构造函数。

有没有一种方法可以在不使用 new 运算符的情况下在堆上创建内容?

您可以使用“malloc”,并在释放内存时使用“free”。否则没有其他办法。

或者,更好的是:我是否没有必要如此紧紧地坚持我的想法?
不应该为结构定义成员函数吗?

就我个人而言,如果我需要我的结构具有成员函数,我会将其更改为类。我倾向于使用结构,就像在 OCaml 中使用记录一样。

In this case, however, is it possible to create a new instance of this struct on the
heap, or would I need to define a constructor?

Yes you can create it on the heap without defining a constructor.

Is there a way to create things on the heap without the new operator?

You can use 'malloc' and when releasing the memory use 'free'. Otherwise there is no other way.

Or, better yet: is it unnecessary for me to cling so tightly to the notion that I
shouldn't define member functions for structs?

Personally if I need my struct to have member functions I change it into a class. I tend to use structs as one might use a record in OCaml.

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