[comp.lang.c++] 2 quick questions

bramwell@Datap.ab.ca (Bob Bramwell) (06/12/91)

These are very short questions.  Maybe the answers will be too :-)

1.  How do I explicitly initialise a private static member variable?
    For example:
	class fred {
		static int fd;		// file descriptor; 0 is legit
	};
    I'd like fred::fd to start life as -1 before the first ever
    constructor invocation on a fred object.  Can this be done?

2.  The following pair of statements appears to be legitimate in C,
    but not in C++:
	typedef char byte;
	unsigned byte thing;
    Obviously what I want is a synonym for char, but C++ doesn't like
    me trying to make it 'unsigned' as well.  Why not?

I look forward to your answers.  Replies via E-mail may not work due
to a (possibly) broken domain name server upstream of us.
-- 
Bob Bramwell	Snail:  10th Floor,	|
			940 6 Av SW,	|   "I love my country, 
403/237-9500w		Calgary, AB, 	|    but I fear my government"
403/283-7367h		T2P 3T1, Canada	|

lijewski@theory.TC.Cornell.EDU (Mike Lijewski) (06/12/91)

In article <1991Jun12.013440.20494@Datap.ab.ca> bramwell@covert.UUCP (Bob Bramwell) writes:
>
>These are very short questions.  Maybe the answers will be too :-)
>
>1.  How do I explicitly initialise a private static member variable?
>    For example:
>	class fred {
>		static int fd;		// file descriptor; 0 is legit
>	};
>    I'd like fred::fd to start life as -1 before the first ever
>    constructor invocation on a fred object.  Can this be done?

Here's an example illustrating what you want:

#include <iostream.h>
class fred {
  static int fd;
  public:
  void p() { cout << fd << endl; }
};

int fred::fd = -1;

int main() { fred x; x.p(); return 0; }

>2.  The following pair of statements appears to be legitimate in C,
>    but not in C++:
>	typedef char byte;
>	unsigned byte thing;
>    Obviously what I want is a synonym for char, but C++ doesn't like
>    me trying to make it 'unsigned' as well.  Why not?

This construct is not legal in ANSI C.

>-- 
>Bob Bramwell	Snail:  10th Floor,	|

-- 
Mike Lijewski  (H)607/272-0238 (W)607/254-8686
Cornell National Supercomputer Facility
ARPA: mjlx@eagle.cnsf.cornell.edu  BITNET: mjlx@cornellf.bitnet
SMAIL:  25 Renwick Heights Road, Ithaca, NY  14850