我可以在堆上创建一个新结构而不定义构造函数吗?
我知道 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的。正如您所说,结构和类之间确实没有重要的区别。如果您被告知要创建一个
struct
,并且它至少需要一个成员函数,那么定义一个成员函数。这确实没什么大不了的。
但除此之外,您当然可以在没有构造函数的情况下堆分配结构(否则
malloc
在 C 中将毫无意义。该语言甚至没有构造函数)
struct S
,您可以简单地调用new S
或new S()
,具体取决于您想要哪种初始化。如果您没有定义构造函数,编译器会为您创建一个。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 callnew S
ornew S()
, depending on what kind of initialization you want. if you don't define a constructor, the compiler makes one for you.正如之前所回答的,您可以通过 new 或使用 malloc 在堆上创建一个新实例。
这是更有趣的问题。 C++ 中的 struct 和 class 之间的主要(唯一?)区别是
默认访问说明符。也就是说,struct 默认为公共访问,class 默认为私有。在我看来,这就是决定您使用两者中哪一个的区别。基本上,如果用户应该直接访问成员,那么它应该是一个
struct
。例如,如果您没有成员函数,那么显然其目的是直接访问对象的成员,因此它将是一个
struct
。如果对象只是用于实现其外部类的一个小的 private 帮助器,如您的示例所示,那么即使它具有成员函数,通常也最清楚地允许外部类访问到它的成员,所以它应该是一个struct
。通常,对于这些类,外部类的实现与内部类的实现紧密耦合,因此没有理由隐藏其中一个类。因此,对于简单的(例如 std::pair)对象或那些使用受到限制的对象(如在私有内部类中),对成员的默认访问可能是一件好事,在这些情况下,我会将它们设为结构体代码>.
As answered before, you can create a new instance on the heap either via new or with malloc.
This is the more interesting question. The major (only?) difference between
struct
andclass
in c++ is thedefault access specifier. That is,
struct
defaults to public access andclass
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 astruct
.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 astruct
. 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
.即使您没有定义构造函数,编译器也会创建一个默认构造函数,因此您可以使用运算符“new”:
AFAIAC,结构是一个类,只是其“公共性”默认值相反。
Even if you don't define a constructor, the compiler will create a default one and so you can use operator 'new':
AFAIAC, a struct is a class, except that its "publicness" default is reversed.
Malloc 工作正常:
只需记住
free()
任何malloc()
'd 和delete
任何new
'd 。Malloc works fine:
Just remember to
free()
anythingmalloc()
'd anddelete
anythingnew
'd.是的,您可以在堆上创建它而无需定义构造函数。
您可以使用“malloc”,并在释放内存时使用“free”。否则没有其他办法。
就我个人而言,如果我需要我的结构具有成员函数,我会将其更改为类。我倾向于使用结构,就像在 OCaml 中使用记录一样。
Yes you can create it on the heap without defining a constructor.
You can use 'malloc' and when releasing the memory use 'free'. Otherwise there is no other way.
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.