⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpp.html

📁 vxworks相关论文
💻 HTML
📖 第 1 页 / 共 5 页
字号:
If you use <SAMP>`#import'</SAMP> <EM>instead of</EM> <SAMP>`#include'</SAMP>, then youdon't need the conditionals inside the header file to prevent multipleexecution of the contents.</P><P><SAMP>`#import'</SAMP> is obsolete because it is not a well designed feature.It requires the users of a header file--the applicationsprogrammers--to know that a certain header file should only be includedonce.  It is much better for the header file's implementor to write thefile so that users don't need to know this.  Using <SAMP>`#ifndef'</SAMP>accomplishes this goal.</P><H3><A NAME="SEC8" HREF="cpp_toc.html#TOC8">Inheritance and Header Files</A></H3><P><A NAME="IDX11"></A><A NAME="IDX12"></A></P><P><EM>Inheritance</EM> is what happens when one object or file derives someof its contents by virtual copying from another object or file.  Inthe case of C header files, inheritance means that one header file includes another header file and then replaces or adds something.</P><P>If the inheriting header file and the base header file have differentnames, then inheritance is straightforward: simply write <SAMP>`#include"<VAR>base</VAR>"'</SAMP> in the inheriting file.</P><P>Sometimes it is necessary to give the inheriting file the same name asthe base file.  This is less straightforward.</P><P>For example, suppose an application program uses the system header file<TT>`sys/signal.h'</TT>, but the version of <TT>`/usr/include/sys/signal.h'</TT>on a particular system doesn't do what the application program expects.It might be convenient to define a "local" version, perhaps under thename <TT>`/usr/local/include/sys/signal.h'</TT>, to override or add to theone supplied by the system.</P><P>You can do this by using the option <SAMP>`-I.'</SAMP> for compilation, andwriting a file <TT>`sys/signal.h'</TT> that does what the applicationprogram expects.  But making this file include the standard<TT>`sys/signal.h'</TT> is not so easy--writing <SAMP>`#include&#60;sys/signal.h&#62;'</SAMP> in that file doesn't work, because it includes your ownversion of the file, not the standard system version.  Used in that fileitself, this leads to an infinite recursion and a fatal error incompilation.</P><P><SAMP>`#include &#60;/usr/include/sys/signal.h&#62;'</SAMP> would find the proper file,but that is not clean, since it makes an assumption about where thesystem header file is found.  This is bad for maintenance, since itmeans that any change in where the system's header files are keptrequires a change somewhere else.</P><P><A NAME="IDX13"></A>The clean way to solve this problem is to use <SAMP>`#include_next'</SAMP>, which means, "Include the <EM>next</EM> file withthis name."  This directive works like <SAMP>`#include'</SAMP> except insearching for the specified file: it starts searching the list of headerfile directories <EM>after</EM> the directory in which the current filewas found.</P><P>Suppose you specify <SAMP>`-I /usr/local/include'</SAMP>, and the list ofdirectories to search also includes <TT>`/usr/include'</TT>; and suppose thatboth directories contain a file named <TT>`sys/signal.h'</TT>.  Ordinary<SAMP>`#include &#60;sys/signal.h&#62;'</SAMP> finds the file under<TT>`/usr/local/include'</TT>.  If that file contains <SAMP>`#include_next&#60;sys/signal.h&#62;'</SAMP>, it starts searching after that directory, and finds thefile in <TT>`/usr/include'</TT>.</P><H2><A NAME="SEC9" HREF="cpp_toc.html#TOC9">Macros</A></H2><P>A macro is a sort of abbreviation which you can define once and thenuse later.  There are many complicated features associated with macrosin the C preprocessor.</P><H3><A NAME="SEC10" HREF="cpp_toc.html#TOC10">Simple Macros</A></H3><P><A NAME="IDX14"></A><A NAME="IDX15"></A></P><P>A <EM>simple macro</EM> is a kind of abbreviation.  It is a name whichstands for a fragment of code.  Some people refer to these as<EM>manifest constants</EM>.</P><P>Before you can use a macro, you must <EM>define</EM> it explicitly with the<SAMP>`#define'</SAMP> directive.  <SAMP>`#define'</SAMP> is followed by the name of themacro and then the code it should be an abbreviation for.  For example,</P><PRE>#define BUFFER_SIZE 1020</PRE><P>defines a macro named <SAMP>`BUFFER_SIZE'</SAMP> as an abbreviation for the text<SAMP>`1020'</SAMP>.  If somewhere after this <SAMP>`#define'</SAMP> directive there comesa C statement of the form</P><PRE>foo = (char *) xmalloc (BUFFER_SIZE);</PRE><P>the C preprocessor recognizes and <EM>expands</EM> the macro<SAMP>`BUFFER_SIZE'</SAMP>, resulting in</P><PRE>foo = (char *) xmalloc (1020);</PRE><P>The use of all upper case for macro names is a standard convention.Programs are easier to read when it is possible to tell at a glance whichnames are macros.</P><P>Normally, a macro definition must be a single line, like all Cpreprocessing directives.  (You can split a long macro definitioncosmetically with Backslash-Newline.)  There is one exception: Newlinescan be included in the macro definition if within a string or characterconstant.  This is because it is not possible for a macro definition tocontain an unbalanced quote character; the definition automaticallyextends to include the matching quote character that ends the string orcharacter constant.  Comments within a macro definition may containNewlines, which make no difference since the comments are entirelyreplaced with Spaces regardless of their contents.</P><P>Aside from the above, there is no restriction on what can go in a macrobody.  Parentheses need not balance.  The body need not resemble valid Ccode.  (But if it does not, you may get error messages from the Ccompiler when you use the macro.)</P><P>The C preprocessor scans your program sequentially, so macro definitionstake effect at the place you write them.  Therefore, the following input tothe C preprocessor</P><PRE>foo = X;#define X 4bar = X;</PRE><P>produces as output</P><PRE>foo = X;bar = 4;</PRE><P>After the preprocessor expands a macro name, the macro's definition body isappended to the front of the remaining input, and the check for macro callscontinues.  Therefore, the macro body can contain calls to other macros.For example, after</P><PRE>#define BUFSIZE 1020#define TABLESIZE BUFSIZE</PRE><P>the name <SAMP>`TABLESIZE'</SAMP> when used in the program would go through twostages of expansion, resulting ultimately in <SAMP>`1020'</SAMP>.</P><P>This is not at all the same as defining <SAMP>`TABLESIZE'</SAMP> to be <SAMP>`1020'</SAMP>.The <SAMP>`#define'</SAMP> for <SAMP>`TABLESIZE'</SAMP> uses exactly the body youspecify--in this case, <SAMP>`BUFSIZE'</SAMP>---and does not check to see whetherit too is the name of a macro.  It's only when you <EM>use</EM> <SAMP>`TABLESIZE'</SAMP>that the result of its expansion is checked for more macro names.See section <A HREF="cpp.html#SEC26">Cascaded Use of Macros</A>.</P><H3><A NAME="SEC11" HREF="cpp_toc.html#TOC11">Macros with Arguments</A></H3><P><A NAME="IDX16"></A><A NAME="IDX17"></A><A NAME="IDX18"></A></P><P>A simple macro always stands for exactly the same text, each time it isused.  Macros can be more flexible when they accept <EM>arguments</EM>.Arguments are fragments of code that you supply each time the macro isused.  These fragments are included in the expansion of the macroaccording to the directions in the macro definition.  A macro thataccepts arguments is called a <EM>function-like macro</EM> because thesyntax for using it looks like a function call.</P><P><A NAME="IDX19"></A>To define a macro that uses arguments, you write a <SAMP>`#define'</SAMP> directivewith a list of <EM>argument names</EM> in parentheses after the name of themacro.  The argument names may be any valid C identifiers, separated bycommas and optionally whitespace.  The open-parenthesis must follow themacro name immediately, with no space in between.</P><P>For example, here is a macro that computes the minimum of two numericvalues, as it is defined in many C programs:</P><PRE>#define min(X, Y)  ((X) &#60; (Y) ? (X) : (Y))</PRE><P>(This is not the best way to define a "minimum" macro in GNU C.See section <A HREF="cpp.html#SEC23">Duplication of Side Effects</A>, for more information.)</P><P>To use a macro that expects arguments, you write the name of the macrofollowed by a list of <EM>actual arguments</EM> in parentheses, separated bycommas.  The number of actual arguments you give must match the number ofarguments the macro expects.   Examples of use of the macro <SAMP>`min'</SAMP>include <SAMP>`min (1, 2)'</SAMP> and <SAMP>`min (x + 28, *p)'</SAMP>.</P><P>The expansion text of the macro depends on the arguments you use.Each of the argument names of the macro is replaced, throughout themacro definition, with the corresponding actual argument.  Using thesame macro <SAMP>`min'</SAMP> defined above, <SAMP>`min (1, 2)'</SAMP> expands into</P><PRE>((1) &#60; (2) ? (1) : (2))</PRE><P>where <SAMP>`1'</SAMP> has been substituted for <SAMP>`X'</SAMP> and <SAMP>`2'</SAMP> for <SAMP>`Y'</SAMP>.</P><P>Likewise, <SAMP>`min (x + 28, *p)'</SAMP> expands into</P><PRE>((x + 28) &#60; (*p) ? (x + 28) : (*p))</PRE><P>Parentheses in the actual arguments must balance; a comma withinparentheses does not end an argument.  However, there is no requirementfor brackets or braces to balance, and they do not prevent a comma fromseparating arguments.  Thus,</P><PRE>macro (array[x = y, x + 1])</PRE><P>passes two arguments to <CODE>macro</CODE>: <SAMP>`array[x = y'</SAMP> and <SAMP>`x +1]'</SAMP>.  If you want to supply <SAMP>`array[x = y, x + 1]'</SAMP> as an argument,you must write it as <SAMP>`array[(x = y, x + 1)]'</SAMP>, which is equivalent Ccode.</P><P>After the actual arguments are substituted into the macro body, the entireresult is appended to the front of the remaining input, and the check formacro calls continues.  Therefore, the actual arguments can contain callsto other macros, either with or without arguments, or even to the samemacro.  The macro body can also contain calls to other macros.  Forexample, <SAMP>`min (min (a, b), c)'</SAMP> expands into this text:</P><PRE>((((a) &#60; (b) ? (a) : (b))) &#60; (c) ? (((a) &#60; (b) ? (a) : (b))) : (c))</PRE><P>(Line breaks shown here for clarity would not actually be generated.)</P><P><A NAME="IDX20"></A><A NAME="IDX21"></A>If a macro <CODE>foo</CODE> takes one argument, and you want to supply anempty argument, you must write at least some whitespace between theparentheses, like this: <SAMP>`foo ( )'</SAMP>.  Just <SAMP>`foo ()'</SAMP> is providingno arguments, which is an error if <CODE>foo</CODE> expects an argument.  But<SAMP>`foo0 ()'</SAMP> is the correct way to call a macro defined to take zeroarguments, like this:</P><PRE>#define foo0() ...</PRE><P>If you use the macro name followed by something other than anopen-parenthesis (after ignoring any spaces, tabs and comments thatfollow), it is not a call to the macro, and the preprocessor does notchange what you have written.  Therefore, it is possible for the same nameto be a variable or function in your program as well as a macro, and youcan choose in each instance whether to refer to the macro (if an actualargument list follows) or the variable or function (if an argument listdoes not follow).</P><P>Such dual use of one name could be confusing and should be avoidedexcept when the two meanings are effectively synonymous: that is, when thename is both a macro and a function and the two have similar effects.  Youcan think of the name simply as a function; use of the name for purposesother than calling it (such as, to take the address) will refer to thefunction, while calls will expand the macro and generate better butequivalent code.  For example, you can use a function named <SAMP>`min'</SAMP> inthe same source file that defines the macro.  If you write <SAMP>`&#38;min'</SAMP> withno argument list, you refer to the function.  If you write <SAMP>`min (x,bb)'</SAMP>, with an argument list, the macro is expanded.  If you write<SAMP>`(min) (a, bb)'</SAMP>, where the name <SAMP>`min'</SAMP> is not followed by anopen-parenthesis, the macro is not expanded, so you wind up with a call tothe function <SAMP>`min'</SAMP>.</P><P>You may not define the same name as both a simple macro and a macro witharguments.</P><P>In the definition of a macro with arguments, the list of argument namesmust follow the macro name immediately with no space in between.  If thereis a space after the macro name, the macro is defined as taking noarguments, and all the rest of the line is taken to be the expansion.  Thereason for this is that it is often useful to define a macro that takes noarguments and whose definition begins with an identifier in parentheses.This rule about spaces makes it possible for you to do either this:</P><PRE>#define FOO(x) - 1 / (x)</PRE><P>(which defines <SAMP>`FOO'</SAMP> to take an argument and expand into minus thereciprocal of that argument) or this:</P><PRE>#define BAR (x) - 1 / (x)</PRE><P>(which defines <SAMP>`BAR'</SAMP> to take no argument and always expand into<SAMP>`(x) - 1 / (x)'</SAMP>).</P><P>Note that the <EM>uses</EM> of a macro with arguments can have spaces beforethe left parenthesis; it's the <EM>definition</EM> where it matters whetherthere is a space.</P>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -