我可以在堆上创建一个新的结构体,而无需定义构造函数? [英] Can I create a new struct on the heap without defining a constructor?

查看:167
本文介绍了我可以在堆上创建一个新的结构体,而无需定义构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道c ++中的结构和类之间有很少的区别(两个?)。尽管如此,我已经被指示使用struct来定义简单的小东西,例如可能不需要成员函数的节点(尽管我可以在技术上包括成员函数)。例如,我可以定义一个节点作为链表类的私有成员,如下所示:

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;

};

在这种情况下,可以在堆上创建一个新的结构实例,或者我需要定义一个构造函数?有没有办法在堆上创建没有新运算符的东西?或者,更好的是:我没有必要紧紧抓住这个概念,我不应该定义结构的成员函数?我应该只是去定义一个吗?或者如果我这样做,是否会承认Node真的应该是一个内部类,而不是一个内部结构?我真的应该担心这些事情吗?

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?

谢谢!

推荐答案


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

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?

如前所述,您可以在堆或者通过new或者使用malloc。

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?

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

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.

如果,例如,你没有成员函数,那么显然目的是为了直接访问对象的成员,因此它将是一个 struct 。在一个对象只是一个小的私人帮助程序实现其外部类,如在你的例子中,那么即使它有成员函数,它通常是最清楚允许外部类访问到它的成员,所以它应该是一个 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 ,对于琐碎的(例如std :: pair)对象或那些使用有限的对象(如在私有内部类中),默认访问成员可能是一件好事,在这些情况下,我会让他们 structs

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.

这篇关于我可以在堆上创建一个新的结构体,而无需定义构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆