static C C++ÓïÒå
´æ´¢ÆÚstorage duration(extent)£¬ ָΪ¶ÔÏóÅäÖõÄÄÚ´æµÄ´æ»îÆÚ£¬±ÈÈçlocal extent
Éú´æ¿Õ¼äscope£¬Ö¸¶ÔÏóÔÚ³ÌÐòÄڵĴæ»îÇøÓò£¬¼´¿É±»·ÃÎʵķ¶Î§£¬±ÈÈçfile scope, static scope, local scope
C
local static object
º¯ÊýÄÚ²¿µÄobject£¬¾ßÓÐlocal scope£¬µ«ÊÇÿ´Îº¯Êý±»µ÷Óúó¸Ã¶ÔÏóµÄÄÚ´æ²»ÇåÀí£¬µ½Ï´ε÷Óû¹±£³ÖÔ×´
file static oject£¬static function
Ö»Óб»±àÒëÎļþµÄÉùÃ÷µãÖ®ºó²ÅÄÜʹÓøÃobjectºÍ¸Ãfunciton£¬´ïµ½Òþ²ØÍⲿ¶ÔÏóµÄÄ¿µÄ
C++
Ö±½Ó¶¨ÒåÔÚÎļþÖеıäÁ¿(ÎÞÂÛÊÇ·ñ¼Óstatic)£¬¾ßÓÐfile scope£¬¼´static scope£¬ÄÚ´æÒ»Ö±µ½³ÌÐò½áÊø²ÅÊÍ·Å
local static object
º¯ÊýÄÚ²¿µÄobject£¬¾ßÓÐlocal scope£¬µ«ÊÇÿ´Îº¯Êý±»µ÷Óúó¸Ã¶ÔÏóµÄÄÚ´æ²»ÇåÀí£¬µ½Ï´ε÷Óû¹±£³ÖÔ×´
static class member
classÄÚΨһµÄÒ»·Ý(¼´¸ú¶ÔÏóÎÞ¹Ø)£¬¿É¹²ÏíµÄ£¬member
const static class member
ͬstatic class member£¬µ«ÊÇ¿ÉÇÒ½ö¿ÉÔÚÉùÃ÷ʱ¸³Öµ
static class functions
²»´æÈ¡ÈκÎnon-static membersµÄº¯Êý
#pragma once
#include <string>
using namespace std;
class MyClass1
{
public:
MyClass1(void);
~MyClass1(void);
static void Test(void); //static class function
private:
static string _name; //static class member
static int _age; //static class member
int _testi;
const static int _size = 1; //const static class member
};
static int mytest2i = 0;
#include "StdAfx.h"
#include "MyClass1.h"
int MyClass1::_age;
string MyClass1::_name = "test"; //initialize here
MyClass1::MyClass1(void)
{
mytest2i = 0;
}
MyClass1::~MyClass1(void)
{
}
void MyClass1::Test(void)
{
MyClass1::_age = 1;
}
Ïà¹ØÎĵµ£º
C++ÄÚ´æ·ÖÅäÃØ¼®—new£¬malloc£¬GlobalAllocÏê½â
......
C/C+ÓïÑÔstructÉî²ã̽Ë÷
×÷ÕߣºËᦻª ³ö´¦£ºPConline
1. structµÄ¾Þ´ó×÷ÓÃ
¡¡¡¡Ãæ¶ÔÒ»¸öÈ˵ĴóÐÍC/C++³ÌÐòʱ£¬Ö ......