Easy/Dumb C++ Question

Programming, for all ages and all languages.
Post Reply
User avatar
astrocrep
Member
Member
Posts: 127
Joined: Sat Apr 21, 2007 7:21 pm

Easy/Dumb C++ Question

Post 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
Mouse Pad - Coming in the distant future...
Kernel: Indigo Kernel - v0.0.1

Thanks to JamesM and BrokenThorn for there tutorials!
iammisc
Member
Member
Posts: 269
Joined: Thu Nov 09, 2006 6:23 pm

Post 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.
Post Reply