Since its called a 'class' I would usually pass it as const reference, but if I use a plain enum it makes no difference right? So does it make a difference if I pass a enum class as value/const ref? Also, does the type matter? For example a enum class:int
An enum type is a distinct value type (§8.3) that declares a set of named constants. declares an enum type named Color with members Red , Green , and Blue .
Your code (e.g. your enum) SHOULD be placed in the . h file if you need to expose it to the code you're including the . h file. However if the enum is only specific to the code in your header's .
An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it.
Difference between Enums and Classes An enum can, just like a class , have attributes and methods. The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden).
enum class
holds an integral value just like a regular enum
so you can safely pass it by value without any overhead. Notice that compiler may sometimes optimize pass by reference as well by replacing it with pass by value. But passing by reference may result in some overhead when such an optimization is not applied.
As a very broad rule of thumb, pass plain old data types as values, and class instances as const
references.
There are exceptions to this rule; and indeed the trendies nowadays like to rely on pass by value followed by move semantics when building copy constructors for example.
For the new enum class
stuff of C++11, pass it by value (after all it only holds an integral type under the hood) and trust the compiler to make the optimisations.
If you are ever in any doubt, profile any performance differences.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With