[comp.lang.c++] New Begginer Questions.

SMITHJ@ohstpy.mps.ohio-state.edu (09/09/89)

I'm following this conversation with interest as I am a somewhat experienced
C programmer looking to learn C++.  I have started reading Stroustrup's 1986
book and have found it to be poorly written based on bad program fragments and
unclear notation.  Would it be best for me to put this book down immediatley
and find another?

Two questions:
1. Is the declaration type* var; common in C++ programs as opposed to type *var
   and if so why.  I find that it misleads one to beleave that the declaration
			type* x, y;
   would be interpreted as
			type *x, *y;
   rather than the correct
			type *x, y;
2. Why the addition of the typecast convention type(var) when (type)var 
   already exists. 

-- 
They have one big advantage over us: 
		*they* know where they're going.
Has your family tried 'em, Powdermilk?

/* Jeffery G. Smith, BS-RHIT (AKA Doc. Insomnia, WMHD-FM)       *
 *    The Ohio State University, Graduate Physics Program       *
 *        3193 Smith Lab, Columbus, OH 43210  (614) 292-5321    *
 *    smithj@ohstpy.mps.ohio-state.edu                          */
-- 
They have one big advantage over us: 
		*they* know where they're going.
Has your family tried 'em, Powdermilk?

/* Jeffery G. Smith, BS-RHIT (AKA Doc. Insomnia, WMHD-FM)       *
 *    The Ohio State University, Graduate Physics Program       *
 *        3193 Smith Lab, Columbus, OH 43210  (614) 292-5321    *
 *    smithj@ohstpy.mps.ohio-state.edu                          */

shopiro@alice.UUCP (Jonathan Shopiro) (09/10/89)

In article <4202@ohstpy.mps.ohio-state.edu>, SMITHJ@ohstpy.mps.ohio-state.edu writes:
> I'm following this conversation with interest as I am a somewhat experienced
> C programmer looking to learn C++.
> 
> Two questions:
> 1. Is the declaration type* var; common in C++ programs as opposed to type *var
>    and if so why.  I find that it misleads one to beleave that the declaration
> 			type* x, y;
>    would be interpreted as
> 			type *x, *y;
>    rather than the correct
> 			type *x, y;
C++ emphasizes the type system.  In C, as in C++, int* is a type, but we
think it more important to say that the variable pi is an int*, as in

	int*	pi;

than to say that the expression *pi is an int, as in

	int	*pi;

Of course the syntax we inherit from C makes multiple declarations
treacherous, as you note, so we generally only declare one variable
at a time.

> 2. Why the addition of the typecast convention type(var) when (type)var 
>    already exists.
We have extended the notion of casting to allow constructing a new value
from arguments.  For example, you might define a type Complex to represent
complex numbers, and allow construction of a Complex from one or two
floats.  You might then write

	float	p, q;  // :-)
	
	(Complex)p;	// cast using old syntax

but you have to use the new syntax for

	Complex(p, q);
-- 
		Jonathan Shopiro
		AT&T Bell Laboratories, Warren, NJ  07060-0908
		research!shopiro   (201) 580-4229

ark@alice.UUCP (Andrew Koenig) (09/11/89)

In article <4202@ohstpy.mps.ohio-state.edu>, SMITHJ@ohstpy.mps.ohio-state.edu writes:

> 1. Is the declaration type* var; common in C++ programs as opposed to type *var
>    and if so why.  I find that it misleads one to beleave that the declaration
> 			type* x, y;
>    would be interpreted as
> 			type *x, *y;
>    rather than the correct
> 			type *x, y;

If you use the style

	type* x;
	
then it would be wise to declare only one variable at a time.
The particular choice of layout is, as usual, a matter of
(sometimes hotly debated) personal preference.  So are indentation
and other such things.

> 2. Why the addition of the typecast convention type(var) when (type)var 
>    already exists. 

So that you can write, for example, Complex(3,4) to mean 3+4i.
-- 
				--Andrew Koenig
				  ark@europa.att.com