Your code looks mostly fine; though usually you would ALLCAPS unscoped enumerations by convention.
However, unscoped enumerations have pretty much been superceded by scoped enumerations. For instance:
enumclassColor{Red,Green,Blue};
This is due to scoping and strong typing. Unscoped enumerations are unscoped, meaning you end polluting the namespace. (This is also why using namespace std; is also not recommended.) In addition, they are implicitly convertable to their underlying type, which can hide bugs.1 Take for instance your above code, but without the operator overload.
#include<iostream>enumColor{black,red,blue,};intmain(){Colorshirt{blue};std::cout<<"Your shirt is "<<shirt;// prints "Your shirt is 2"return0;}
This is probably not what you want. With a scoped enumeration, the equivelant code would cause a compilation error.
#include<iostream>enumclassColor{black,red,blue,};intmain(){Colorshirt=Color::blue;std::cout<<"Your shirt is "<<shirt;// error: invalid operands to binary expression ('basic_ostream<char, char_traits<char>>' and 'Color')return0;}
As an aside, did you know you can enable syntax highlighting by putting the language name after the three backticks?
I've just started coding and just found this interesting to start covering about. Next week there should be one on either scoped enumeration (or struct) so I'll get to learn about what you're talking about. And the thing about the syntax highlighting is much appreciated as this is my first blog/post ever on any type of social media. Have a nice day!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Your code looks mostly fine; though usually you would ALLCAPS unscoped enumerations by convention.
However, unscoped enumerations have pretty much been superceded by scoped enumerations. For instance:
This is due to scoping and strong typing. Unscoped enumerations are unscoped, meaning you end polluting the namespace. (This is also why
using namespace std;is also not recommended.) In addition, they are implicitly convertable to their underlying type, which can hide bugs.1 Take for instance your above code, but without the operator overload.This is probably not what you want. With a scoped enumeration, the equivelant code would cause a compilation error.
As an aside, did you know you can enable syntax highlighting by putting the language name after the three backticks?
This is sometimes useful, however] ↩
I've just started coding and just found this interesting to start covering about. Next week there should be one on either scoped enumeration (or struct) so I'll get to learn about what you're talking about. And the thing about the syntax highlighting is much appreciated as this is my first blog/post ever on any type of social media. Have a nice day!