Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

cÓëc++ staticº¯ÊýµÄÇø±ð

 
cÓëc++ staticº¯ÊýµÄÇø±ð

c
Óëc++ static
º¯ÊýµÄÇø±ð
static
¹Ø¼ü×ÖÊÇ
C, C++
Öж¼´æÔڵĹؼü×Ö
,
ËüÖ÷ÒªÓÐÈýÖÖʹÓ÷½Ê½
,
ÆäÖÐÇ°Á½ÖÖÖ»Ö¸ÔÚ
C
ÓïÑÔÖÐʹÓÃ
,
µÚÈýÖÖÔÚ
C++
ÖÐʹÓÃ
(C,C++
ÖоßÌåϸ΢²Ù×÷²»¾¡Ïàͬ
,
±¾ÎÄÒÔ
C++
Ϊ׼
).
(1)
¾Ö²¿¾²Ì¬±äÁ¿
(2)
Íⲿ¾²Ì¬±äÁ¿
/
º¯Êý
(3)
¾²Ì¬Êý¾Ý³ÉÔ±
/
³ÉÔ±º¯Êý
ÏÂÃæ¾ÍÕâÈýÖÖʹÓ÷½Ê½¼°×¢ÒâÊÂÏî·Ö±ð˵Ã÷
Ò»¡¢¾Ö²¿¾²Ì¬±äÁ¿
ÔÚ
C/C++
ÖÐ
,
¾Ö²¿±äÁ¿°´ÕÕ´æ´¢ÐÎʽ¿É·ÖΪÈýÖÖ
auto, static, register
(<C
ÓïÑÔ³ÌÐòÉè¼Æ
(
µÚ¶þ°æ
)>
Ì·ºÆÇ¿
,
µÚ
174-175
Ò³
)
Óë
auto
ÀàÐÍ
(
ÆÕͨ
)
¾Ö²¿±äÁ¿Ïà±È
, static
¾Ö²¿±äÁ¿ÓÐÈýµã²»Í¬
1.
´æ´¢¿Õ¼ä·ÖÅ䲻ͬ
auto
ÀàÐÍ·ÖÅäÔÚÕ»ÉÏ
,
ÊôÓÚ¶¯Ì¬´æ´¢Àà±ð
,
Õ¼¶¯Ì¬´æ´¢Çø¿Õ¼ä
,
º¯Êýµ÷ÓýáÊøºó×Ô¶¯ÊÍ·Å
,
¶ø
static
·ÖÅäÔÚ¾²Ì¬´æ´¢Çø
,
ÔÚ³ÌÐòÕû¸öÔËÐÐÆڼ䶼²»ÊÍ·Å
.
Á½ÕßÖ®¼äµÄ×÷ÓÃÓòÏàͬ
,
µ«Éú´æÆÚ²»Í¬
.
2. static
¾Ö²¿±äÁ¿ÔÚËù´¦Ä£¿éÔÚ³õ´ÎÔËÐÐʱ½øÐгõʼ»¯¹¤×÷
,
ÇÒÖ»²Ù×÷Ò»´Î
3.
¶ÔÓÚ¾Ö²¿¾²Ì¬±äÁ¿
,
Èç¹û²»¸³³õÖµ
,
±àÒëÆÚ»á×Ô¶¯¸³³õÖµ
0
»ò¿Õ×Ö·û
,
¶ø
auto
ÀàÐ͵ijõÖµÊDz»È·¶¨µÄ
. (
¶ÔÓÚ
C++
ÖеÄ
class
¶ÔÏóÀýÍâ
, class
µÄ¶ÔÏóʵÀýÈç¹û²»³õʼ»¯
,
Ôò»á×Ô¶¯µ÷ÓÃĬÈϹ¹Ô캯Êý
,
²»¹ÜÊÇ·ñÊÇ
static
ÀàÐÍ
)
Ìصã
: static
¾Ö²¿±äÁ¿µÄ”¼ÇÒäÐÔ”ÓëÉú´æÆڵĔȫ¾ÖÐÔ”
Ëùν”¼ÇÒäÐÔ”ÊÇÖ¸ÔÚÁ½´Îº¯Êýµ÷ÓÃʱ
,
ÔÚµÚ¶þ´Îµ÷ÓýøÈëʱ
,
Äܱ£³ÖµÚÒ»´Îµ÷ÓÃÍ˳öʱµÄÖµ
.
ʾÀý³ÌÐòÒ»
#include <iostream>
using namespace std;
void staticLocalVar()
{
static int a = 0; //
ÔËÐÐÆÚʱ³õʼ»¯Ò»´Î
,
Ï´ÎÔÙµ÷ÓÃʱ
,
²»½øÐгõʼ»¯¹¤×÷
cout<<"a="<<a<<endl;
++a;
}
int main()
{
staticLocalVar(); //
µÚÒ»´Îµ÷ÓÃ
,
Êä³ö
a=0
staticLocalVar(); //
µÚ¶þ´Îµ÷ÓÃ
,
¼ÇÒäÁ˵ÚÒ»´ÎÍ˳öʱµÄÖµ
,
Êä³ö
a=1
return 0;
}
Ó¦ÓÃ
:
ÀûÓÔ¼ÇÒäÐÔ”
,
¼Ç¼º¯Êýµ÷ÓõĴÎÊý
(
ʾÀý³ÌÐòÒ»
)
ÀûÓÃÉú´æÆڵĔȫ¾ÖÐÔ”
,
¸ÄÉÆ”
return a pointer / reference to a local object
”µÄÎÊÌâ
. Local object
µÄÎÊÌâÔÚÓÚÍ˳öº¯Êý
,
Éú´æÆÚ¼´½áÊø
,.
À


Ïà¹ØÎĵµ£º

C_ʹÓÃbreakºÍcontinue¿ØÖÆÑ­»·Óï¾ä

 Ô´Â룺
# include <stdio.h>
 
int main( )
{
    int radius;
    double area;
    for(radius = 1; radius <= 10 ; radius++)
    {
        area = 3.1416 * radius * radius;
   ......

C_¶¨Òå×Ö·ûÐͶþάÊý×éʵÏÖ¼òµ¥µÄ±à¼­Æ÷

 Ô´Â룺
# include <stdio.h>
/* ºê¶¨Òå */
# define MAX 100
# define LEN 80
 
/* Ò»¸ö·Ç³£¼òµ¥µÄÎı¾±à¼­Æ÷ */
int main()
{
    char text[MAX][LEN];  // ¶¨Òå×Ö·ûÐÍÊý×é
    register int t, i, j;    /* ¶¨ÒåÈý¸ö¼Ä´æÆ÷±äÁ¿ */
  ......

¡¶C³ÌÐòÉè¼ÆÓïÑÔ¡·¶ÁÊé±Ê¼Ç20091106

     ÊéÖÐÓм¸¸öÎÊÌâÓеãÄ£ºý¡£¼Ç¼һÏ¡£
    
Answer to Exercise 1-7
Write a program to print the value of EOF .
 
#include <stdio.h>
 
int main(void)
{
  printf("The value of EOF is %d\n\n", EOF);
 
  return 0 ......

C/C++µÄÌõ¼þ±àÒë

ÒÔΪÒѾ­ÕÆÎÕÌõ¼þ±àÒ룬Ԥ±àÒëµÄÒªÁìÁË£¬½á¹û»¹ÊÇ´æÔںܶàÒÅ©µÄµØ·½£¬ËùÒÔ¹æÕûһϣº
Ìõ¼þ±àÒ룺
1
#ifdef _XXXX
...³ÌÐò¶Î1...
#else
...³ÌÐò¶Î2...
#endif
  Õâ±íÃ÷Èç¹û±êʶ·û_XXXXÒѱ»#defineÃüÁÒå¹ýÔò¶Ô³ÌÐò¶Î1½øÐбàÒ룻·ñÔò¶Ô³ÌÐò¶Î2½øÐбàÒë¡£
2
#ifndef _XXXX
...³ÌÐò¶Î1...
#else
...³ÌÐ ......

(ת)³¹µ×¸ã¶¨CÖ¸Õ룭º¯ÊýÃûÓ뺯ÊýÖ¸Õë

 £¨×ª×Ô£©http://blog.pfan.cn/whyhappy/6030.html
 º¯ÊýÃûÓ뺯ÊýÖ¸Õë
Ò» ͨ³£µÄº¯Êýµ÷ÓÃ
    Ò»¸öͨ³£µÄº¯Êýµ÷ÓõÄÀý×Ó£º
//×ÔÐаüº¬Í·Îļþ
void MyFun(int x);    //´Ë´¦µÄÉêÃ÷Ò²¿Éд³É£ºvoid MyFun( int );
int main(int argc, char* argv[])
{
   ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ