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 .
Chris hopman
source share