Can I create a new struct on the heap without defining a constructor?

拜拜、爱过 提交于 2019-12-04 05:48:20

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.

Malloc works fine:

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

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

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!