A<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B%2FLanguage%2FStatic</id>
		<title>C++/Language/Static - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B%2FLanguage%2FStatic"/>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B/Language/Static&amp;action=history"/>
		<updated>2026-04-19T02:05:16Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.cppe.ru/index.php?title=C%2B%2B/Language/Static&amp;diff=1889&amp;oldid=prev</id>
		<title> в 14:21, 25 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B/Language/Static&amp;diff=1889&amp;oldid=prev"/>
				<updated>2010-05-25T14:21:06Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 14:21, 25 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://www.cppe.ru/index.php?title=C%2B%2B/Language/Static&amp;diff=1890&amp;oldid=prev</id>
		<title>Admin: 1 версия:&amp;#32;Импорт контента...</title>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B/Language/Static&amp;diff=1890&amp;oldid=prev"/>
				<updated>2010-05-25T10:28:06Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия: Импорт контента...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==A shared resource example.==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;cstring&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class output {&lt;br /&gt;
  static char sharedResource[255]; // this is the shared resource&lt;br /&gt;
  static int inuse;        // buffer available if 0; in use otherwise&lt;br /&gt;
  static int oindex;       // index of sharedResource&lt;br /&gt;
  char str[80];&lt;br /&gt;
  int i;                   // index of next char in str&lt;br /&gt;
  int who;                 // identifies the object, must be &amp;gt; 0&lt;br /&gt;
public:&lt;br /&gt;
  output(int w, char *s) { &lt;br /&gt;
     strcpy(str, s); &lt;br /&gt;
     i = 0; &lt;br /&gt;
     who = w; &lt;br /&gt;
  }&lt;br /&gt;
  int putbuf() &lt;br /&gt;
  {&lt;br /&gt;
    if(!str[ i ]) {         // done outputting&lt;br /&gt;
      inuse = 0;            // release buffer&lt;br /&gt;
      return 0;             // signal termination&lt;br /&gt;
    }&lt;br /&gt;
    if(!inuse)              // get buffer&lt;br /&gt;
        inuse = who; &lt;br /&gt;
    if(inuse != who)        // in use by someone else&lt;br /&gt;
        return -1; &lt;br /&gt;
    if(str[ i ]) {          // still chars to output&lt;br /&gt;
      sharedResource[oindex] = str[ i ];&lt;br /&gt;
      i++; oindex++;&lt;br /&gt;
      sharedResource[oindex] = &amp;quot;\0&amp;quot;;// always keep null-terminated&lt;br /&gt;
      return 1;&lt;br /&gt;
    }&lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
  void show() { &lt;br /&gt;
     cout &amp;lt;&amp;lt; sharedResource &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
char output::sharedResource[255];   // this is the shared resource&lt;br /&gt;
int output::inuse = 0;      // buffer available if 0; in use otherwise&lt;br /&gt;
int output::oindex = 0;     // index of sharedResource&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  output object1(1, &amp;quot;This is a test&amp;quot;), object2(2, &amp;quot; of statics&amp;quot;);&lt;br /&gt;
  while(object1.putbuf() | object2.putbuf()) ; // output chars&lt;br /&gt;
  object1.show();&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A static member variable example.==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class myclass {&lt;br /&gt;
  static int i;&lt;br /&gt;
public:&lt;br /&gt;
  void setInt(int n) { &lt;br /&gt;
     i = n; &lt;br /&gt;
  }&lt;br /&gt;
  int getInt() { &lt;br /&gt;
     return i; &lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
int myclass::i;           // Definition of myclass::i. i is still private to myclass.&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  myclass object1, object2;&lt;br /&gt;
  object1.setInt(10); &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;object1.i: &amp;quot; &amp;lt;&amp;lt; object1.getInt() &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; // displays 10&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;object2.i: &amp;quot; &amp;lt;&amp;lt; object2.getInt() &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; // also displays 10 &lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Init static data before object creation==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class StaticMemberClass {&lt;br /&gt;
  static int i;&lt;br /&gt;
public:&lt;br /&gt;
  static void init(int x) {&lt;br /&gt;
     i = x;&lt;br /&gt;
  }&lt;br /&gt;
  void show() {&lt;br /&gt;
     cout &amp;lt;&amp;lt; i;&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
int StaticMemberClass::i; // define i&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  &lt;br /&gt;
  StaticMemberClass::init(100);&lt;br /&gt;
  StaticMemberClass x;&lt;br /&gt;
  x.show(); &lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Static function and static variable==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class MyClass {&lt;br /&gt;
  static int i;&lt;br /&gt;
public:&lt;br /&gt;
  static void init(int x) {&lt;br /&gt;
     i = x;&lt;br /&gt;
  }&lt;br /&gt;
  void show() {&lt;br /&gt;
     cout &amp;lt;&amp;lt; i;&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
int MyClass::i;&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  // init static data before object creation&lt;br /&gt;
  MyClass::init(30);&lt;br /&gt;
  MyClass x;&lt;br /&gt;
  x.show();&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Static function variable==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
void printMessage(void);  &lt;br /&gt;
int main ()&lt;br /&gt;
{&lt;br /&gt;
   char choice;&lt;br /&gt;
   do {&lt;br /&gt;
      cout &amp;lt;&amp;lt; &amp;quot;Enter Q to quit, any other character to continue: &amp;quot;;&lt;br /&gt;
      cin &amp;gt;&amp;gt; choice;&lt;br /&gt;
      if (choice == &amp;quot;Q&amp;quot;)&lt;br /&gt;
         cout &amp;lt;&amp;lt; &amp;quot;Input stopped&amp;quot;;&lt;br /&gt;
      else&lt;br /&gt;
         printMessage(); &lt;br /&gt;
      } while (choice != &amp;quot;Q&amp;quot;);&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
void printMessage (void)&lt;br /&gt;
{&lt;br /&gt;
   static int times = 0;&lt;br /&gt;
   times++;&lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;This function called &amp;quot; &amp;lt;&amp;lt; times &amp;lt;&amp;lt; &amp;quot; times\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Static Member Functions: its strictions==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
//A static member function does not have a this pointer. &lt;br /&gt;
//cannot be a static and a non-static version of the same function. &lt;br /&gt;
//A static member function may not be virtual. &lt;br /&gt;
//static functions cannot be declared as const or volatile.&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class MyClass {&lt;br /&gt;
  static int resource;&lt;br /&gt;
public:&lt;br /&gt;
  static int getResource();&lt;br /&gt;
  void freeResource() { &lt;br /&gt;
     resource = 0; &lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
int MyClass::resource;   // define resource&lt;br /&gt;
int MyClass::getResource()&lt;br /&gt;
{&lt;br /&gt;
  if(resource) return 0; // resource already in use&lt;br /&gt;
  else {&lt;br /&gt;
    resource = 1;&lt;br /&gt;
    return 1;            // resource allocated to this object&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  MyClass myObject1, myObject2;&lt;br /&gt;
  if(MyClass::getResource()) &lt;br /&gt;
     cout &amp;lt;&amp;lt; &amp;quot;myObject1 has resource\n&amp;quot;;&lt;br /&gt;
  if(!MyClass::getResource()) &lt;br /&gt;
     cout &amp;lt;&amp;lt; &amp;quot;myObject2 denied resource\n&amp;quot;;&lt;br /&gt;
  myObject1.freeResource();&lt;br /&gt;
  &lt;br /&gt;
  if(myObject2.getResource()) &lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;myObject2 can now use resource\n&amp;quot;;&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Static member functions: &amp;quot;preinitialize&amp;quot; private static data==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class static_type {&lt;br /&gt;
  static int i;&lt;br /&gt;
public:&lt;br /&gt;
  static void init(int x) {&lt;br /&gt;
     i = x;&lt;br /&gt;
  }&lt;br /&gt;
  void show() {&lt;br /&gt;
     cout &amp;lt;&amp;lt; i;&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
int static_type::i;&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  &lt;br /&gt;
  static_type::init(100);   // init static data before object creation&lt;br /&gt;
  static_type x;&lt;br /&gt;
  x.show(); &lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Usage and effect of a static data member==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class shared {&lt;br /&gt;
  static int a;&lt;br /&gt;
  int b;&lt;br /&gt;
public:&lt;br /&gt;
  void set(int i, int j) {&lt;br /&gt;
    a=i; &lt;br /&gt;
    &lt;br /&gt;
    b=j;&lt;br /&gt;
  }&lt;br /&gt;
  void show();&lt;br /&gt;
} ;&lt;br /&gt;
int shared::a;&lt;br /&gt;
void shared::show()&lt;br /&gt;
{&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;This is static a: &amp;quot; &amp;lt;&amp;lt; a;&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;\nThis is non-static b: &amp;quot; &amp;lt;&amp;lt; b;&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  shared x, y;&lt;br /&gt;
  x.set(1, 1); &lt;br /&gt;
  x.show();&lt;br /&gt;
  y.set(2, 2); &lt;br /&gt;
  y.show();&lt;br /&gt;
  x.show(); &lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Use a static member variable independent of any object.==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class myclass {&lt;br /&gt;
public:&lt;br /&gt;
  static int i;&lt;br /&gt;
  void setInt(int n) { &lt;br /&gt;
     i = n; &lt;br /&gt;
  }&lt;br /&gt;
  int getInt() { &lt;br /&gt;
     return i; &lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
int myclass::i;&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  myclass object1, object2;&lt;br /&gt;
  &lt;br /&gt;
  myclass::i = 100;                      // set i directly,  no object is referenced.&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;object1.i: &amp;quot; &amp;lt;&amp;lt; object1.getInt() &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; // displays 100&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;object2.i: &amp;quot; &amp;lt;&amp;lt; object2.getInt() &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; // also displays 100&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>