[ Pobierz całość w formacie PDF ]

extern  C int printf ( const char ", ... );
This instructs the compiler not to use the C++ name mangling rules on this
function, but instead to use the C rules. However, by doing this, theprintf
function may not be overloaded. This provides the easiest way to interface
C++ and assembly, define the function to use C linkage and then use the C
calling convention.
For convenience, C++ also allows the linkage of a block of functions
and global variables to be defined. The block is denoted by the usual curly
braces.
extern  C {
/" C linkage global variables and function prototypes "/
}
If one examines the ANSI C header files that come with C/C++ com-
pilers today, they will find the following near the top of each header file:
#ifdef cplusplus
extern  C {
#endif
And a similar construction near the bottom containing a closing curly brace.
C++ compilers define the cplusplus macro (with two leading under-
scores). The snippet above encloses the entire header file within anextern "C"
block if the header file is compiled as C++, but does nothing if compiled
as C (since a C compiler would give a syntax error forextern "C"). This
same technique can be used by any programmer to create a header file for
assembly routines that can be used with either C or C++.
7.2.2 References
References are another new feature of C++. They allow one to pass
parameters to functions without explicitly using pointers. For example,
consider the code in Figure 7.11. Actually, reference parameters are pretty
154 CHAPTER 7. STRUCTURES AND C++
1 void f ( int & x ) // the & denotes a reference parameter
2 { x++; }
3
4 int main()
5 {
6 int y = 5;
7 f(y ); // reference to y is passed , note no & here!
8 printf ( %d\n , y); // prints out 6!
9 return 0;
10 }
Figure 7.11: Reference example
simple, they really are just pointers. The compiler just hides this from
the programmer (just as Pascal compilers implementvarparameters as
pointers). When the compiler generates assembly for the function call on
line 7, it passes the address ofy. If one was writing functionfin assembly,
they would act as if the prototype was6:
void f ( int " xp);
References are just a convenience that are especially useful for opera-
tor overloading. This is another feature of C++ that allows one to define
meanings for common operators on structure or class types. For example, a
common use is to define the plus (+) operator to concatenate string objects.
Thus, ifaandbwere strings,a + bwould return the concatenation of the
stringsaandb. C++ would actually call a function to do this (in fact, these
expression could be rewritten in function notation asoperator +(a,b)).
For efficiency, one would like to pass the address of the string objects in-
stead of passing them by value. Without references, this could be done as
operator +(&a,&b), but this would require one to write in operator syntax
as&a + &b. This would be very awkward and confusing. However, by using
references, one can write it asa + b, which looks very natural.
7.2.3 Inline functions
Inline functions are yet another feature of C++7. Inline functions are
meant to replace the error-prone, preprocessor-based macros that take pa-
rameters. Recall from C, that writing a macro that squares a number might
look like:
6
Of course, they might want to declare the function with C linkage to avoid name
mangling as discussed in Section 7.2.1
7
C compilers often support this feature as an extension of ANSI C.
7.2. ASSEMBLY AND C++ 155
1 inline int inline f ( int x )
2 { return x"x; }
3
4 int f ( int x )
5 { return x"x; }
6
7 int main()
8 {
9 int y , x = 5;
10 y = f(x);
11 y = inline f (x);
12 return 0;
13 }
Figure 7.12: Inlining example
#define SQR(x) ((x)"(x))
Because the preprocessor does not understand C and does simple sub-
stitutions, the parenthesis are required to compute the correct answer in
most cases. However, even this version will not give the correct answer for
SQR(x++).
Macros are used because they eliminate the overhead of making a func-
tion call for a simple function. As the chapter on subprograms demonstrated,
performing a function call involves several steps. For a very simple function,
the time it takes to make the function call may be more than the time to
actually perform the operations in the function! Inline functions are a much
more friendly way to write code that looks like a normal function, but that
does notCALLa common block of code. Instead, calls to inline functions are [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • anielska.pev.pl