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

I understand that in C ++ (two?) There are very few differences between structures and classes. Be that as it may, I was instructed to use structures to define simple little things, such as nodes, that might not need member functions (even though I could include member functions). For example, I can define 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 structure on the heap, or will I need to define a constructor? Is there a way to create things on the heap without a new statement? Or, even better: do I need to adhere so closely to the notion that I should not define member functions for structures? Should I just go ahead and define it? Or, if I did, would it be recognized that node really should be an inner class, not an inner structure? Should I really worry about these things at all? What is more readable?

Thanks!

+7
c ++ memory-management heap coding-style struct
source share
6 answers

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

As already mentioned, you can create a new instance on the heap either through new or using malloc.

Or, even better: do I not have to stick so closely to the notion that I should not define member functions for structures?

This is a more interesting question. The main (only?) Difference between struct and class in C ++ is the default access specifier. That is, by default, struct is used for shared access, and class is used for private access. In my opinion, this is a difference that should determine which of the two you are using. Basically, if users should access members directly, then it should be a struct .

If, for example, you do not have member functions, then obviously the intention is to have object elements accessed directly, and therefore it would be a struct . In the case of an object that is a small private helper for implementing its outer class, as in your example, then even if it has member functions, it is often clear that access to the outer class is to its members, and therefore it must be struct . Often with these classes the implementation of the outer class is closely related to the implementation of the inner class, and therefore there is no reason to hide it from another.

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

+5
source share

Malloc works fine:

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

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

+4
source share

Even if you do not define a constructor, the compiler will create a default value, and you can use the 'new' operator:

 Node *n = new Node; 

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

+2
source share

In this case, however, can I create a new instance of this structure on the heap, or do I need to define a constructor?

Yes, you can create it on a heap without specifying a constructor.

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

You can use "malloc", and when freeing memory, use "free". Otherwise there is no other way.

Or, even better: I do not need to cling so tightly to the fact that I should not define member functions for structures?

Personally, if I need my structure in order to have member functions, I change it to a class. I usually use structures, since you can use the entry in OCaml.

+2
source share

Should I really worry about these things at all?

Yes. As you say, there really are no important differences between structures and classes. If you were told to do a struct and need at least one member function, then define a member function.

It really doesn’t matter.

But besides this, you can, of course, allocate heaps of structures without constructors (otherwise malloc will be quite meaningless in C. This language does not even have constructors)

Given struct S , you can simply call new S or new S() , depending on which initialization you want. if you do not define a constructor, the compiler does this for you.

+1
source share

In c ++, a structure will implicitly initialize structure elements with zeros, for example:

 struct Rectangle { int length; int width; }; // Create dynamic struct object using pointer struct Rectangle *dps; dps = new Rectangle; cout << endl << (*dps).length << " x " << (*dps).width << endl; 

This code will return 0 x 0

0
source share

All Articles