[comp.lang.c++] Define's and Devices.

ing@hades.OZ (Ian Gold) (11/25/89)

	I would like to write object orientated code in C++ but am having 
trouble coping with two concepts. Perhaps my C background is plaguing me.  
Perhaps I am a hypocondriac.

	The first problem is the "#define syndrome".  Assume I have a keyboard
defined as

	enum keys { ENTER = 0x13,
		    ESC   = 0x27,
		    A_CHR = 0x41,
		    B_CHR = 0x42,
			.
			.
			.
		  };

In C I would declare this globally so everybody knows about it.  What I
would like to do in C++ is something like

	class keys
	{
	public:
		enum keys { ENTER = 0x13 
				.
				.
				.
			  };
	};

	class program : keys
	{
	public:
		void program :: function(void) { return keys::ENTER; }
	}

and allow it to be inherited only by the classes that need it.  I realise
that this is much slower than using #defines and would only be useful if
one uses multiple inheritance.  The problem is that the above example is
NOT C++.  DOES ANYBODY KNOW HOW OR IF I CAN ACHIEVE THE ABOVE IDEAS EFFICIENTY
IN C++ !  Is the following code the best way out.

	class keys
	{
	public:
		int ENTER() { return 0x13; } 
			.
			.
			.
	};

	class program : keys
	{
	public:
		void program :: function(void) { return keys::ENTER(); }
	}


	My second problem is that of devices.  Say I have three CPU's
2 keyboards and half a VDU.  Somewhere I must declare these as being the
only devices avaliable.  IS THE FOLLOWING CODE THE "CORRECT" WAY TO HANDLE
THE SITUATION.

	class program
	{
		CPU 		cpu1;
		CPU 		cpu2;
		CPU		cpu3;
		KEYBOARD	keyboard1;
		KEYBOARD	keyboard2;
		VCU		vdu(0.5);	
		boolean		program();
	public:
		go() { keyboard1.reset(); 
		       keyboard2.reset(); 
		       vdu.reset();
		       program();         }
	};

	void main(void)
	{
		program a;

		a.go();
	}

pcg@emerald.cs.aber.ac.uk (Piercarlo Grandi) (11/28/89)

In article <478@hades.OZ> ing@hades.OZ (Ian Gold) writes:


	   The first problem is the "#define syndrome".  Assume I have a keyboard
   defined as

	   enum keys { ENTER = 0x13,
		       ESC   = 0x27,
		       A_CHR = 0x41,
		       B_CHR = 0x42,
			   .
			   .
			   .
		     };

   In C I would declare this globally so everybody knows about it.  What I
   would like to do in C++ is something like

	   class keys
	   {
	   public:
		   enum keys { ENTER = 0x13 
				   .
				   .
				   .
			     };
	   };

	   class program : keys
	   {
	   public:
		   void program :: function(void) { return keys::ENTER; }
	   }

   and allow it to be inherited only by the classes that need it.
   The problem is that the above example is NOT C++.


Actually it is C++ 2.0; in C++ 1.2 etc... you can instead use
static consts, in some compilers at least [in 1.2 you cannot
initialize a class static, but a const often can...].

   I realise
   that this is much slower than using #defines and would only be useful if
   one uses multiple inheritance.

Without discussing this, neither assertion is necessarily true. Use enums,
not #defines, in C++ 2.0.


	   My second problem is that of devices.  Say I have three CPU's
   2 keyboards and half a VDU.  Somewhere I must declare these as being the
   only devices avaliable.  IS THE FOLLOWING CODE THE "CORRECT" WAY TO HANDLE
   THE SITUATION.

	   class program
	   {
		   CPU 		cpu1;
		   CPU 		cpu2;
		   CPU		cpu3;
		   KEYBOARD	keyboard1;
		   KEYBOARD	keyboard2;
		   VCU		vdu(0.5);	
		   boolean		program();
	   public:
		   go() { keyboard1.reset(); 
			  keyboard2.reset(); 
			  vdu.reset();
			  program();         }
	   };

This is debatable. Function/control decomposition need not be
mapped onto data abstraction. This may well be excessive
application of Object Orientedness.
--
Piercarlo "Peter" Grandi           | ARPA: pcg%cs.aber.ac.uk@nsfnet-relay.ac.uk
Dept of CS, UCW Aberystwyth        | UUCP: ...!mcvax!ukc!aber-cs!pcg
Penglais, Aberystwyth SY23 3BZ, UK | INET: pcg@cs.aber.ac.uk

rae%alias@csri.utoronto.ca (Reid Ellis) (11/29/89)

ing@hades.OZ (Ian Gold) writes:
|would like to do in C++ is something like
|
|	class keys
|	{
|	public:
|		enum keys { ENTER = 0x13 
|				.
|				.
|				.
|			  };
|	};
|
|	class program : keys
|	{
|	public:
|		void program :: function(void) { return keys::ENTER; }
|	}
|
|and allow it to be inherited only by the classes that need it.

This is one of the first things I tried to do in C++ too, but I did this
instead:
	class keys
		{
		public:
		const ENTER = 0x13;
		// ...
		};

	class program : keys
		{
		public:
			int function(void) { return ENTER; }
		// ...
		}

But I was rewarded with a "not yet implemented" error message :-/

I would hope that static const's are now "implemented" with cfront 2.0.

					Reid

[btw, I found your syntax for the "program" class method 'function' a bit
 cryptic -- hope I didn't misread what you wanted here.]

---
Reid Ellis, 264 Broadway Avenue, Toronto ON, M4P 1V9, Canada
rae%alias@csri.utoronto.ca                   +1 416 487 1383