Topic: Strange C++ diffs from ANSI C
Author: ajrooks@vlsi.waterloo.edu (Alan Rooks)
Date: Mon, 16 Mar 1992 22:10:14 GMT Raw View
There are two specific areas in which the definitions of C++ and ANSI C
differ that I don't understand. I've checked the ARM and the FAQ list,
and seen nothing explaining these two:
1) In section 3.1 of the reference manual, it is stated that the following
are definitions:
struct S { int a; int b; };
enum { up, down };
Aside: anyone else notice that, in The C++ Programming Language, 2nd ed, it
is stated in section 2.1, page 44, that a typedef is a definition, whereas
in the reference manual, section r.3.1, it is stated that typedef is just
a declaration?
It is also understood that this is a definition:
inline int zero(void) { return 0; }
My problem is that all of the above definitions commonly occur in header
files, which are #included into several of the source files in a program.
But it is also stated in section 3.1 that there must be exactly one
definition of each object, function, class, and enumerator used in a
program. How does this not conflict with the use of #include to put several
copies of these kinds of definition in a program?
2) The C++ reference manual says that the results of the pre-increment,
pre-decrement, conditional, assignment, and comma operators are lvalues
(at least under certain conditions) whereas in ANSI C these are all *not*
lvalues. The following code illustrates:
#include <iostream.h>
int k = 0;
main()
{
int i, j;
(i = 3) = 4; // this is not C
++++++++++++++++++++i; // neither is this
(i, j) = 2; // nor this
(k) ? i : j += 3; // nor this
cout << i << " " << j << "\n"; // prints "14 5"
}
The only one of these that I can see as being remotely useful is the
conditional operator. Is there any reason for these (to the ignorant,
possibly) seemingly gratuitous differences from C?
Thanks for any enlightenment!
Alan Rooks University of Waterloo ajrooks@vlsi.uwaterloo.ca