[comp.lang.c++] references and casts

mkhaw@teknowledge-vaxc.ARPA (Mike Khaw) (01/25/89)

I have the following program:

	extern int	printf(char*, ...);
	enum sym { FIRST, SECOND, THIRD };

	main()
	{
		void	func(int&);

		sym	foo = THIRD;
		int	far = 0;
		int&	boo = (int) foo;
		int&	bar = far;

		func(boo);
		printf("foo = %d, boo = %d\n", foo, boo);

		func(bar);
		printf("far = %d, bar = %d\n", far, bar);
	}

	void func(int& i)
	{
		i = 1;
		return;
	}

Why does Oasys C++ 1.2 yield

	foo = 2, boo = 1
	far = 1, bar = 1

I expected "foo = 1, boo = 1".  Do I misunderstand how references
work?

Thanks,
Mike Khaw
-- 
internet: mkhaw@teknowledge.com
uucp:	  {uunet|sun|ucbvax|decwrl|ames|hplabs}!mkhaw%teknowledge.com
hardcopy: Teknowledge Inc, 1850 Embarcadero Rd, POB 10119, Palo Alto, CA 94303

tom@tnosoes.UUCP (Tom Vijlbrief) (01/26/89)

mkhaw@teknowledge-vaxc.ARPA (Mike Khaw) writes:

>I have the following program:

Lines deleted...

>		sym	foo = THIRD;
>		int	far = 0;
>		int&	boo = (int) foo;
>		int&	bar = far;

>I expected "foo = 1, boo = 1".  Do I misunderstand how references
>work?

The declaration:

		int&	boo = (int) foo;

will NOT declare boo to reference variable foo. 
"boo" will reference a integer valued expression. It is
as if you had written:

int	xtemp= (int) foo;
int&	boo= xtemp;

Changing foo will not change boo.


Tom
===============================================================================
Tom Vijlbrief
TNO Institute for Perception
P.O. Box 23				Phone: +31 34 63 62 77
3769 ZG  Soesterberg			E-mail: tnosoes!tom@mcvax.cwi.nl
The Netherlands				    or:	uunet!mcvax!tnosoes!tom
===============================================================================