From fdf8d8eacbfb088a320f41f21cc148f879fe57a8 Mon Sep 17 00:00:00 2001 From: light-city <455954986@qq.com> Date: Sun, 14 Jul 2019 20:39:17 +0800 Subject: [PATCH] update dir --- static/READNE.md | 247 +++++++++++++++++++++++++++++++ static/demo | Bin 0 -> 9104 bytes static/nostatic_class.cpp | 28 ++++ static/static_class.cpp | 28 ++++ static/static_demo.cpp | 24 +++ static/static_error_variable.cpp | 26 ++++ static/static_funciton.cpp | 20 +++ static/static_variable.cpp | 24 +++ 8 files changed, 397 insertions(+) create mode 100644 static/READNE.md create mode 100755 static/demo create mode 100644 static/nostatic_class.cpp create mode 100644 static/static_class.cpp create mode 100644 static/static_demo.cpp create mode 100644 static/static_error_variable.cpp create mode 100644 static/static_funciton.cpp create mode 100644 static/static_variable.cpp diff --git a/static/READNE.md b/static/READNE.md new file mode 100644 index 0000000..f2964ce --- /dev/null +++ b/static/READNE.md @@ -0,0 +1,247 @@ +# C ++中的静态关键字 + +## 关于作者 + +微信公众号: + +![](../img/wechat.jpg) + +当与不同类型一起使用时,Static关键字具有不同的含义。我们可以使用static关键字: + +**静态变量:**函数中的变量,类中的变量 +**静态类的成员**:类对象和类中的函数 + +现在让我们详细看一下静态的这些用法: + +**静态变量** + +- 函数中的静态变量 + +当变量声明为static时,空间**将在程序的生命周期内分配**。即使多次调用该函数,静态变量的空间也**只分配一次**,前一次调用中的变量值通过下一次函数调用传递。这对于在C / C ++或需要存储先前函数状态的任何其他应用程序非常有用。 + +```c++ +#include +#include +using namespace std; + +void demo() +{ + // static variable + static int count = 0; + cout << count << " "; + + // value is updated and + // will be carried to next + // function calls + count++; +} + +int main() +{ + for (int i=0; i<5; i++) + demo(); + return 0; +} +``` + +输出: + +``` +0 1 2 3 4 +``` + +您可以在上面的程序中看到变量count被声明为static。因此,它的值通过函数调用来传递。每次调用函数时,都不会对变量计数进行初始化。 + +- 类中的静态变量 + +由于声明为static的变量只被初始化一次,因为它们在单独的静态存储中分配了空间,因此类中的静态变量**由对象共享。**对于不同的对象,不能有相同静态变量的多个副本。也是因为这个原因,静态变量不能使用构造函数初始化。 + +```c++ + +#include +using namespace std; + +class Apple +{ +public: + static int i; + + Apple() + { + // Do nothing + }; +}; + +int main() +{ +Apple obj1; +Apple obj2; +obj1.i =2; +obj2.i = 3; + +// prints value of i +cout << obj1.i<<" "< +using namespace std; + +class Apple +{ +public: + static int i; + + Apple() + { + // Do nothing + }; +}; + +int Apple::i = 1; + +int main() +{ + Apple obj; + // prints value of i + cout << obj.i; +} +``` + +输出: + +``` +1 +``` + +**静态成员** + +- 类对象为静态 + +就像变量一样,对象也在声明为static时具有范围,直到程序的生命周期。 + +考虑以下程序,其中对象是非静态的。 + +```c++ +#include +using namespace std; + +class Apple +{ + int i; + public: + Apple() + { + i = 0; + cout << "Inside Constructor\n"; + } + ~Apple() + { + cout << "Inside Destructor\n"; + } +}; + +int main() +{ + int x = 0; + if (x==0) + { + Apple obj; + } + cout << "End of main\n"; +} + +``` + + +输出: + +```c++ +Inside Constructor +Inside Destructor +End of main +``` + +在上面的程序中,对象在if块内声明为非静态。因此,变量的范围仅在if块内。因此,当创建对象时,将调用构造函数,并且在if块的控制权越过析构函数的同时调用,因为对象的范围仅在声明它的if块内。 +如果我们将对象声明为静态,现在让我们看看输出的变化。 + +```c++ +#include +using namespace std; + +class Apple +{ + int i; + public: + Apple() + { + i = 0; + cout << "Inside Constructor\n"; + } + ~Apple() + { + cout << "Inside Destructor\n"; + } +}; + +int main() +{ + int x = 0; + if (x==0) + { + static Apple obj; + } + cout << "End of main\n"; +} + +``` + + +输出: + +``` +Inside Constructor +End of main +Inside Destructor +``` + +您可以清楚地看到输出的变化。现在,在main结束后调用析构函数。这是因为静态对象的范围是贯穿程序的生命周期。 + +- 类中的静态函数 + +就像类中的静态数据成员或静态变量一样,静态成员函数也不依赖于类的对象。我们被允许使用对象和'.'来调用静态成员函数。但建议使用类名和范围解析运算符调用静态成员。 + +允许静态成员函数仅访问静态数据成员或其他静态成员函数,它们无法访问类的非静态数据成员或成员函数。 + +```c++ +#include +using namespace std; + +class Apple +{ + public: + // static member function + static void printMsg() + { + cout<<"Welcome to Apple!"; + } +}; + +// main function +int main() +{ + // invoking a static member function + Apple::printMsg(); +} +``` + +输出: + +``` +Welcome to Apple! +``` + diff --git a/static/demo b/static/demo new file mode 100755 index 0000000000000000000000000000000000000000..fdd7a26994d71699bb7e0d3ce8f176a51e58c4c2 GIT binary patch literal 9104 zcmeHNdu*H46~A_#^x-(rLP!zy7_d?lle9^ejsc(ZXzea-L*f<&D>shqBnHPe_UA6G zU<;&lMB`D7{gG*!H0|-n{(}9njZIUNu87AUlVBPW8WZXeAh*2AV_p7$dcSkOd+g7j zoegc$q)D9U`_4VTd(OG<``vq-Zx0Rcbh})F$t`XcB+WG%3bNipytqk;8tW4m2}5ia zSBOR+4R{QdL)=O+U(v3aH*38*@OnIbZVTvh8!Ey43JXRF7I6)UQ+Z`FbkgEqU1N#N zh43kW?5M6Q>y-#Tt`f{|*Y%jAiYfA`H^F4r=20T}xJocTtt-Zy+edAqC#?O3-3BC* z4Ps(Yy~~_*iALTuW0YRhjux8@f+1UcSV=-KXS-WrNA>)yqsV2`Pq)yzTS@BZhtQA-=^A4{z-S&-u|8N!j%~kO2zIGiXgY5Sv@FVviq1x4#Ej1-Otg?P zbPH!9iImti9O@sC+x*-7Jy!bm+vRqDx9%V}<{Qm8nptl3rVJPUUIpj*l5bC7iq(mF z-LI5SNnS7Jb>G9vbPMkHm6mbjT6+W!1<5=027@rmF=%Vt9%hn(R2GtH-1!{WuB4=?hG;x!-R z-Llz0isWg^Jaf7Ty5=m&)YLOCD*1JisZ-A^EBP-ZQ`62orQ|=6OieoTVwXv(QmEL$XB(~sksq79;O2-tDU`UL4N(fAaeq}nN$Cyt^wmuOBF?jcp` z6{~*&w*?La7K(xWf$;F7*Syu>L+3o&@gDeCXyJTl;pO4$sHV-qlb_ZVe)DOu7<}km z)7SMX+Yc|C9bPy;2oJ>#e+|x`as_w1oj*$x>&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*By5T-46KPV1*xe;5I7f%Wcwc{D7$*4%8ImfVC*{u3z@8yFw0Dp~n5s88 zjkvKK3TlPm;AqLgzMVVAhQcx&=pP;;ql(JMDghN86>{+Fdjoqy1ISYm#b_?CdI$2q zkD=C%dK!j`Me6utRisj9>rkbX<*243r`kKkS{>g1RTvjR)c+v(>MUR%1~QgN$$T!3 z9;EVWhlXV=Vn(cpEX9L+R5YhAlTD<|J-I0g5=%sxc|1A5>nKgea%nk@IYMFR@`zZo zN>1c*TtJ*k6T+W6JYz;Cz|E}1r#Xdk1&Gv|e4)F*sq#GPRbOWx?sYqfkRJk(opkbx51!9tgGGkNyyTEch8LVzt5imoGY> zV9P3fxA@%6ztizOjf){&JADyC6CP~Cio$L~bH#+uVx>2K7Z~V5!fwy!cvCyPniVy7 z+7ALhkGm9?zvwoU=As6+D5-p(#EHFv_Clh&Bik>#4M+m_1I0wMZZO?HDarOYX(&2? zH@5bYJ>MUmClJKQ|~C5yuw_ z+MeSER<2c7P7`0iW4HG<8U*9sq?j1DSn1922?^?K9e*>&p(D@{b($eBPVW z_8*dS8*#Y|{S*m%`5aF%bwM0gV0{`ZJN*Iz+4K1E`D;$w^Za2Or~NM-_WZju{~k>- zc{@4nmyl^MpYMkSU4GcnerNx^0DGLqO7R&!kMr--&i-=RzYNr7H45acFU!o|hmO7e zrTefhz#u0!Vmsz5$g|t?{l?JtTu*kvb}U~&f-r9XxIS->Xu~$$e%AMKLi0N0lS-!p zXZs!p&hD7EBA?5)tDe*w2mO{|Cx@uY=Cxzjdsx@MnLbXSEAec@!|kWNjEL4}t@4lS M0AiOT(^>vM0n>|1lK=n! literal 0 HcmV?d00001 diff --git a/static/nostatic_class.cpp b/static/nostatic_class.cpp new file mode 100644 index 0000000..b4e56d2 --- /dev/null +++ b/static/nostatic_class.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; + +class Apple +{ + int i; + public: + Apple() + { + i = 0; + cout << "Inside Constructor\n"; + } + ~Apple() + { + cout << "Inside Destructor\n"; + } +}; + +int main() +{ + int x = 0; + if (x==0) + { + Apple obj; + } + cout << "End of main\n"; +} + diff --git a/static/static_class.cpp b/static/static_class.cpp new file mode 100644 index 0000000..007e400 --- /dev/null +++ b/static/static_class.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; + +class Apple +{ + int i; + public: + Apple() + { + i = 0; + cout << "Inside Constructor\n"; + } + ~Apple() + { + cout << "Inside Destructor\n"; + } +}; + +int main() +{ + int x = 0; + if (x==0) + { + static Apple obj; + } + cout << "End of main\n"; +} + diff --git a/static/static_demo.cpp b/static/static_demo.cpp new file mode 100644 index 0000000..1910088 --- /dev/null +++ b/static/static_demo.cpp @@ -0,0 +1,24 @@ +// the use of static Static +// variables in a Function +#include +#include +using namespace std; + +void demo() +{ + // static variable + static int count = 0; + cout << count << " "; + + // value is updated and + // will be carried to next + // function calls + count++; +} + +int main() +{ + for (int i=0; i<5; i++) + demo(); + return 0; +} diff --git a/static/static_error_variable.cpp b/static/static_error_variable.cpp new file mode 100644 index 0000000..47b1319 --- /dev/null +++ b/static/static_error_variable.cpp @@ -0,0 +1,26 @@ +// variables inside a class + +#include +using namespace std; + +class Apple +{ + public: + static int i; + + Apple() + { + // Do nothing + }; +}; + +int main() +{ + Apple obj1; + Apple obj2; + obj1.i =2; + obj2.i = 3; + + // prints value of i + cout << obj1.i<<" "< +using namespace std; + +class Apple +{ + public: + // static member function + static void printMsg() + { + cout<<"Welcome to Apple!"; + } +}; + +// main function +int main() +{ + // invoking a static member function + Apple::printMsg(); +} + diff --git a/static/static_variable.cpp b/static/static_variable.cpp new file mode 100644 index 0000000..b14780e --- /dev/null +++ b/static/static_variable.cpp @@ -0,0 +1,24 @@ +// variables inside a class + +#include +using namespace std; + +class GfG +{ + public: + static int i; + + GfG() + { + // Do nothing + }; +}; + +int GfG::i = 1; + +int main() +{ + GfG obj; + // prints value of i + cout << obj.i; +}