Page 1 of 1

Easy/Dumb C++ Question

Posted: Sat Feb 16, 2008 7:42 pm
by astrocrep
Consider the Following Class definition:

Code: Select all

class Console
{
public:
	enum Colors	
	{
		BLACK = 0, 
		DARK_GREY = 8,
		BLUE = 1,
		LIGHT_BLUE = 9,
		GREEN = 2,  
		LIGHT_GREEN = 10,
		CYAN = 3,
		LIGHT_CYAN = 11,
		RED = 4,  
		LIGHT_RED = 12,
		MAGENTA = 5,
		LIGHT_MAGENTA = 13,
		BROWN = 6,
		LIGHT_BROWN = 14,
		LIGHT_GREY = 7,
		WHITE = 15
	};
	
	Console()	{ this->console_active = 0; }
	//Init the console window... by default we need a size...
	Console(u8int start_x, u8int start_y, u8int width, u8int height);
	~Console();
	//Sets the active Foreground Color
	void set_forecolor(Colors fore_color);
	//Sets the active Background Color
	void set_bgcolor(Colors bg_color);
	//Prints a string to the screen
	void prints(const char *s);
	void clear();
};
and then I use it like so...

Code: Select all

Console term1;

void main()
{
  term1.set_forecolor(Console::GREEN);
}
Why is that correct instead of:

Code: Select all

term1.set_forecolor(Console::Colors::GREEN);
I am using a G++ cross compiler (elf output, os dev)

Thanks in advance,
Rich

Posted: Sat Feb 16, 2008 10:52 pm
by iammisc
If you had a plain enum not in a class definition:

Code: Select all

   enum Colors   
   {
      BLACK = 0,
      DARK_GREY = 8,
      BLUE = 1,
      LIGHT_BLUE = 9,
      GREEN = 2, 
      LIGHT_GREEN = 10,
      CYAN = 3,
      LIGHT_CYAN = 11,
      RED = 4, 
      LIGHT_RED = 12,
      MAGENTA = 5,
      LIGHT_MAGENTA = 13,
      BROWN = 6,
      LIGHT_BROWN = 14,
      LIGHT_GREY = 7,
      WHITE = 15
   }; 
would you use it like this:

Code: Select all

Colors color = Colors::GREEN;
or like this

Code: Select all

Colors color = GREEN;
You should have chosen the second one.

Exact same idea with classes. Enumerations don't get their own namespace.