I'm studying enumeration at the minute and i've a quick question. In the following code using enum, are you limited to ouputting the sequence number of the possible values, or can you use the enum operator to output the text "green"?

#include <iostream>
#include <sstream>
#include <fstream>
#include "conio.h"
using namespace std;

int main()
{	
	enum colour {red, green, blue};
	colour favourite;
	favourite = green;

	cout << favourite;

	_getch();
}

Dani AI

Generated

Short answer: yes — streaming an unscoped enum (like in the original post) prints its underlying numeric value because there is no built-in ostream text representation. showed overloading operator<< and suggested a helper function; both are correct. A slightly more modern and maintainable pattern (C++11+) is to use a scoped enum class and a small to_string helper that keeps the mapping next to the enum.

enum class Colour { Red, Green, Blue, Count }; // Count is a sentinel

constexpr const char* to_string(Colour c) noexcept {
    static constexpr const char* names[] = { "Red", "Green", "Blue" };
    auto i = static_cast<std::size_t>(c);
    return i < std::size(names) ? names[i] : "Unknown";
}

// usage: std::cout << to_string(Colour::Green) << '\n';

Notes and practical tips:

  • The array approach above is fast and compact but requires contiguous zero-based values (or a sentinel). If enumerators have explicit or sparse values, prefer a switch (robust, readable) or an associative container for rare conversions.
  • Small types: prefer passing enums by value, not const & — copying an enum is typically cheaper and clearer than a reference. That addresses ’s question about reference vs value.
  • If targeting older compilers, enum class may not be available; then either overload operator<< or use a mapping function as already suggested by and .
  • Avoid nonstandard headers like <conio.h> for portable examples; use standard I/O and input helpers instead.

For many enums across a codebase, consider an X-macro pattern or a small code-generation/script to avoid repeating names. Also, if compile-time reflection is desired, research modern libraries that automate enum-name extraction.

Recommended Answers

All 5 Replies

That's a new one 'enum operator'. What's that?

Do you mean something like below?

#include <iostream>

enum colour {red, green, blue};

std::ostream& operator <<(std::ostream & out, const colour & c)
{
  switch (c)
  {
	case red:
	  return out << "red";
	case green:
	  return out << "green";
	case blue:
	  return out << "blue";
  }
  return out;
}

int main()
{	
	
	colour favourite;
	favourite = green;

	std::cout << favourite << std::endl;

	return 0;
}

gerard4143 code is very smart, maybe advanced.
enum is a constant name, with an integer value, so if you like to print the name of it self, you have to make a function, or higher level Operator.

if I do it with a function I will keep your code, and do next

#include <iostream>
#include <sstream>
#include <fstream>
#include "conio.h"
using namespace std;

string getPrintStr(int colorEnum)
{
switch (colorEnum)
  {
	case red:
	  return "red";
	case green:
	  return "green";
	case blue:
	  return "blue";
  }
return "unknown color";
}

int main()
{	
	enum colour {red, green, blue};
	colour favourite;
	favourite = green;

	cout << getPrintStr(favourite);

	_getch();
}
string getPrintStr(int colorEnum)
{
switch (colorEnum)
  {
	case red:
	  return "red";
	case green:
	  return "green";
	case blue:
	  return "blue";
  }
return "unknown color";
}

Your function should really be.

string getPrintStr(const colour & colorEnum)
{
switch (colorEnum)
  {
	case red:
	  return "red";
	case green:
	  return "green";
	case blue:
	  return "blue";
  }
return "unknown color";
}

thanks for the feedback

I dont think it fail if they both run, but I dont get why you passing the parameter by reference, and const
it also can be like the next without any problem,
enums are constant integers so you are very flexable to sent,
either enum colour or int,

string getPrintStr(enum colour colorEnum)

Your function should really be.

string getPrintStr(const colour & colorEnum)
{
switch (colorEnum)
  {
	case red:
	  return "red";
	case green:
	  return "green";
	case blue:
	  return "blue";
  }
return "unknown color";
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.