之前對extern、static的一些東西一直模棱兩可。今天好好來梳理了一番。。ios
被static修飾的變量或函數稱之爲靜態成員、函數。
存儲位置:static修飾的變量存放在靜態區(全局區)。一般有如下特色:函數
靜態局部變量與靜態全局變量不一樣的一點:this
定義在函數體外部的變量稱之爲全局變量。
存儲位置:一樣存放在靜態區中。
特色:spa
與靜態變量的區別:code
做用:將被修飾變量聲明爲外部變量對象
//f.c #include <stdio.h> void f(int* a ) { printf("f.c : this is test\n" ); printf("f.c : a=%d\n", ++*a); }
//main.cpp #include <iostream> using namespace std; extern "C" void f( int*); int main( ) { int a = 10; f(&a); cout<<"now"<<a<<endl; return 0; }
1. 若只是簡單的調用C++的函數、重載函數時。將要調用的函數封裝便可blog
//C++ Code #include <iostream> using namespace std; void f(int i) { cout<<"f:i= "<<i<<endl; } void f(double d) { cout<<"f:d= "<<d<<endl; } extern "C" void f_i(int i) { f(i); } extern "C" void f_d(double d) { f(d); }
//C Code #include <stdio.h> int main( ) { f_i(10); f_d(10.111); return 0; }
2. 若想要訪問C++代碼中的類對象。 此時就須要將整個類都封裝起來,對外提供C形式接口,和C++相關方法封裝在自定義的結構中接口
stu.h:get
#ifndef __STU_H_ #define __STU_H_ typedef struct C_Stu C_Stu; #ifdef __cplusplus extern "C" { #endif //__cpluscplus C_Stu* getInstance(void); void releaseInstance( C_Stu** pp); void Call_Stu_func(C_Stu* p); #ifdef __cplusplus }; #endif //__cpluscplus #endif //__STU_H_
C Code:io
//C Code #include <stdio.h> #include "stu.h" int main( ) { C_Stu* p = getInstance(); Call_Stu_func(p); releaseInstance(&p); }
C++ Code:
// #include <iostream> using namespace std; class Stu { public: Stu() {cout<<"Stu()"<<endl;} virtual void func() { cout<<"virtual func"<<endl; } ~Stu() { cout<<"~Stu()"<<endl; } }; #ifdef __cplusplus extern "C" { #endif //__cpluscplus typedef struct C_Stu{ Stu s; }C_Stu; C_Stu* getInstance(void) { return new C_Stu; } void releaseInstance(C_Stu** pp) { delete *pp; *pp = NULL; } void Call_Stu_func(C_Stu* p) { p->s.func(); } #ifdef __cplusplus }; #endif //__cpluscplus
結果: