Group Group Group Group Group Group Group Group Group

Question about struct vs class


Hello All,

I have question regarding the choice of struct vs class in some of the data structure.
Why the book has some data structures defined using struct like heap but some are defined as class like LinkedList. What is the reason behind it? I know basics of the difference between both but just curious to know why one vs the other in book?

Thanks in advance.


@kelvin_lau @jomoka Can you please help with this when you get a chance? Thank you - much appreciated! :]



There are various reasons why we chose to define the type as a struct or a class:

  1. If both definitions yield identical behavior, we preferred the struct definition.
  2. Some implementations can give free copy-on-write behavior (default behavior in STL). In those situations, we choose structs to get that for free.
  3. Recursive types must use classes or indirect enumerations. This is because recursive types have undefined size at compile time.