When should I define a type as a struct or as a class?
I know that struct are value types while classes are reference types. So I wonder, for example, should I define a stack as a struct or a class?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Reason #1 to choose struct vs class: classes have inheritance, structs do not. If you need polymorphism, you must use classes.
Reason #2: structs are normally value types (though you can make them reference types if you work at it). Classes are always reference types. So, if you want a value type, choose a struct. If you want a reference type, it’s easiest to go with a class.
Reason #3: If you have a type with a lot of data members, then you’re probably going to want a reference type (to avoid expensive copying), in which case, you’re probably going to choose a class.
Reason #4: If you want deterministic destruction of your type, then it’s going to need to be a struct on the stack. Nothing on the GC heap has deterministic destruction, and the destructiors/finalizers of stuff on the GC heap may never be run. If they’re collected by the GC, then their finalizers will be run, but otherwise, they won’t. So, if you want your type to automatically be destroyed when it leaves scope, you need to use a struct and put it on the stack.
As for your particular case, containers should normally be reference types (copying all of their elements every time that you pass one around would be insanely expensive), and a
Stackis a container, so you’re going to want to use a class unless you want to go to the trouble of making it a ref-counted struct, which is decidedly more work. It just has the advantage of guaranteeing that its destructor will run when it’s not used anymore.On a side note, if you create a container which is a class, you’re probably going to want to make it final so that its various functions can be inlined (and won’t be virtual if that class doesn’t derive from anything other than
Objectand they’re not functions thatObjecthas), which can be important for something like a container where performance can definitely matter.