函数内部静态变量,( \\Visual C++\\ CLR \\ windows窗体应用程序 编写C++)

来源:互联网 发布:java经典书籍排行 编辑:程序博客网 时间:2024/04/29 13:04
#pragma oncenamespace demo2 {using namespace System;using namespace System::ComponentModel;using namespace System::Collections;using namespace System::Windows::Forms;using namespace System::Data;using namespace System::Drawing;/// <summary>/// Form1 摘要/// </summary>public ref class Form1 : public System::Windows::Forms::Form{public:Form1(void){InitializeComponent();////TODO: 在此处添加构造函数代码//}protected:/// <summary>/// 清理所有正在使用的资源。/// </summary>~Form1(){if (components){delete components;}}private: System::Windows::Forms::Button^  button1;protected: private: System::Windows::Forms::Button^  button2;private: System::Windows::Forms::Label^  label1;private: System::Windows::Forms::Label^  label2;private:/// <summary>/// 必需的设计器变量。/// </summary>System::ComponentModel::Container ^components;#pragma region Windows Form Designer generated code/// <summary>/// 设计器支持所需的方法 - 不要/// 使用代码编辑器修改此方法的内容。/// </summary>void InitializeComponent(void){this->button1 = (gcnew System::Windows::Forms::Button());this->button2 = (gcnew System::Windows::Forms::Button());this->label1 = (gcnew System::Windows::Forms::Label());this->label2 = (gcnew System::Windows::Forms::Label());this->SuspendLayout();// // button1// this->button1->Location = System::Drawing::Point(73, 178);this->button1->Name = L"button1";this->button1->Size = System::Drawing::Size(75, 23);this->button1->TabIndex = 0;this->button1->Text = L"button1";this->button1->UseVisualStyleBackColor = true;this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);// // button2// this->button2->Location = System::Drawing::Point(204, 178);this->button2->Name = L"button2";this->button2->Size = System::Drawing::Size(75, 23);this->button2->TabIndex = 1;this->button2->Text = L"button2";this->button2->UseVisualStyleBackColor = true;this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);// // label1// this->label1->AutoSize = true;this->label1->Location = System::Drawing::Point(89, 91);this->label1->Name = L"label1";this->label1->Size = System::Drawing::Size(41, 12);this->label1->TabIndex = 2;this->label1->Text = L"label1";// // label2// this->label2->AutoSize = true;this->label2->Location = System::Drawing::Point(238, 91);this->label2->Name = L"label2";this->label2->Size = System::Drawing::Size(41, 12);this->label2->TabIndex = 3;this->label2->Text = L"label2";// // Form1// this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;this->ClientSize = System::Drawing::Size(395, 291);this->Controls->Add(this->label2);this->Controls->Add(this->label1);this->Controls->Add(this->button2);this->Controls->Add(this->button1);this->Name = L"Form1";this->Text = L"Form1";this->ResumeLayout(false);this->PerformLayout();}#pragma endregionprivate: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) { // MessageBox::Show("hello xiao cui"); static int a =0;  // 这个是静态的没按一次button1 就加加一次,这个是在静态分配区, a++; this->label1->Text = a.ToString(); // 这个是将label1改成1, }             // 这是一个私有的成员函数,private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) { static int b = 0; // int b = 0; // 这个不是静态的每次按button2 还是1,这个是在堆栈上分配内存空间,将其加加, // 每执行一次就又从b = 0 ,开始进行计算, b++; this->label2->Text = b.ToString(); }};}
#include <iostream>using namespace std;char *a = "xiaocuiwoxiangni";char oneChar(const char *charArray){static const char *s = charArray;  // 这个就是静态变量,if(*s == '\0')return 0;return *s++;}int main(){char c;while((c = oneChar(a)) != 0)cout << c << " ";cout << endl;system("pause");return 0;}


0 0
原创粉丝点击