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;
}
相关文档:
1、在cpp文件中调用c文件中实现的函数的时候,需要用extern "C"声明该函数,否则cpp会按名字改编后的
函数名去找该函数而找不到。
cpp文件调用c文件中函数如下:
c文件中有一函数:
void Transfer(int a; char b);
&nbs ......
C++内存分配秘籍—new,malloc,GlobalAlloc详解
......
C语言函数的出口:return语句(高质量c/c++编程指南) 收藏
今天看到了一篇关于c/c++语言中,对于函数出口处:return语句的正确性和效率性的检查问题。平时我们都不太看重return语句的,认为它简单,不是很重要,其实不然,return语句要是写的不好,很容易导致效率低下,甚至会出错!特别是在处理指针时。
下面看看要 ......