Static 静态成员的说明

博主进行测试后发现,除了Application可用于全局性变量应用,static也能实现。文中给出了BaseConfig类中使用static定义多个变量的示例,还展示了密码验证、用户信息处理及在线信息管理等相关代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天在做了一下测试,以前在网上只是看到过,没有测试,一直一来就觉得全局性变量的应用只有Application,其时static也可以用到!例如:

public class BaseConfig
 {
  public static string system_reged = "非法用户";

  public static bool log_system = true;
  public static bool log_error = true;
  public static int public_pbc = 5;
  public static int public_title = 2;
  public static int public_html = 10;
  public static int public_pbc_imgheight = 100;
  public static int public_pbc_imgwidth = 200;
  public static int public_fightgrade = 100;
  public static ArrayList public_roomlist = new ArrayList();
  public static Hashtable public_onlineinfo = new Hashtable();
  public static int public_online = 0;
  public static int public_inchat = 0;
  public static string[] public_goldmax = new string[4];
  public static int[] public_t1 = new int[4];
  public static int[] public_t2 = new int[4];
  public static int[] public_t3 = new int[4];
  public static int[] public_t4 = new int[4];
  public static string public_adminclass = "六扇门";
  public static bool public_adminfree = true;
  public static ArrayList public_randomhappen = new ArrayList();
  public static string public_streettype;
  public static DataTable public_street;
  public static x.CheckUse public_song;
  public static x.CheckUse public_webicq;
  public static x.CheckUse public_fly;
  public static x.CheckUse public_bet;
  public static int public_question = 6;
  public static DataTable public_horseinfo;
  public static DataTable public_shopthing;
  public static string public_shoptype;
  public static int public_horsespan = 180;
  public static Hashtable public_horselist = new Hashtable();
  public static int public_horseID = 1;
  public static Hashtable public_questionlist = new Hashtable();
  public static int public_questionid = 0;
 }

第二步通过

    password = FormsAuthentication.HashPasswordForStoringInConfigFile(password + dr["注册IP"], "SHA1");
    if (dr["密码"].ToString() != password)
    {
     dr.Close();
     MyConn.Close();
     MyConn.Dispose();
     this.byts_errormsg.Text = "密码错误!请仔细核对!";
    }
    else
    {
     x.UserInfo info = new x.UserInfo();
     info.UserName = username;
     info.DataID = dr["id"].ToString();
     info.RoomID = "false";
     info.LastSave = DateTime.Now;
     info.MsgLine = 0;
     info.LastSaid = DateTime.Now;
     info.Sex = dr["性别"].ToString();
     info.Ico = (int) dr["头像"];
     info.Grade = (int) dr["等级"];
  ......

     dr.Close();
     string myonline = username + "|" + info.DataID + "|" + info.Sex + "|" + info.Ico + "|" + info.Grade + "|" + info.FGrade + "|" + info.Gold + "|" + info.CloseDoor + "|" + info.MyClass + "|" + info.MyHead + "|" + info.MyJob;
   
 

 this.Application.Lock();
     if (var.public_onlineinfo.ContainsKey(username) == true)
     {
      MyConn.Close();
      MyConn.Dispose();
      this.byts_errormsg.Text = "该用户名正在使用,不能重复登陆!";
      return;
     }
     var.public_onlineinfo.Add(username, myonline);
     var.public_online ++;
     this.Application.UnLock();

}

<think>好的,我现在需要帮助用户了解C++中static静态函数的使用方法和特性。首先,我要回忆一下static在C++中的相关知识,确保自己理解正确。根据用户提供的引用内容,特别是引用[1]和引用[3],static成员函数是属于类而不是实例的,它们不能访问非静态成员,也不能使用this指针。另外,引用[3]中的例子展示了如何在静态函数中通过保存this指针来间接调用非静态成员函数,这可能是一个需要注意的点。 首先,我应该明确静态函数的基本定义和语法,比如在类内声明时使用static关键字,然后在类外定义时不需要重复static。同时,要强调静态函数只能访问静态成员变量或其他静态函数,不能直接访问类的非静态成员,这一点可能需要通过例子说明。 接下来,特性部分需要分点说明。比如,独立于实例、访问权限限制、作用域限定、回调函数中的应用等。每个特性都要结合引用中的例子,比如引用[1]提到的静态函数不依赖于实例,引用[3]中的回调函数使用静态函数并通过静态指针访问非静态成员。 然后,使用场景部分,可以举单例模式、工具类函数、回调函数封装等例子,这样用户能更好地理解何时使用静态函数。比如引用[3]中的例子,静态函数作为回调,通过保存的this指针调用成员函数,这在实际编程中很常见。 注意事项方面,要提到静态函数不能声明为const或volatile,因为它们没有this指针。另外,多线程环境下静态成员变量的同步问题也需要提醒用户注意,避免竞态条件。 用户可能的问题可能包括如何调用静态函数,与普通成员函数的区别,以及为什么静态函数不能访问非静态成员。这些问题需要提前预判,并在回答中涵盖。例如,调用方式可以通过类名直接调用,而无需实例,这点在引用[1]的示例中已经体现。 最后,生成相关问题时要围绕静态函数的相关知识点展开,比如与其他静态成员的关系、设计模式中的应用、多线程问题等,这样用户可以有进一步学习的方向。需要确保问题有针对性,帮助用户深入理解。 在整理回答时,要确保结构清晰,分点明确,每个部分都有引用支持,比如引用[1]、[3]的具体内容。同时,保持语言简洁,避免技术术语过多导致用户难以理解。可能还需要比较静态函数和普通成员函数的不同,帮助用户建立对比。 现在,需要检查是否有遗漏的重要点,比如静态函数的初始化、生命周期等,但根据用户提供的引用,可能不需要深入这些方面,主要聚焦在使用方法和特性上。因此,保持回答集中在用户的问题范围内,不扩展过多无关内容。 总结来说,回答的结构应该是:定义、使用方法、特性、使用场景、注意事项、示例代码、相关问题。每个部分都结合引用内容,确保准确性和相关性。这样用户能够系统地掌握static静态函数的关键知识点。</think>### C++ static静态函数的使用方法与特性 #### 一、静态函数的定义与使用方法 1. **语法格式** 在类内部声明时添加`static`关键字,类外定义时不再重复使用`static`: ```cpp class MyClass { public: static void myStaticFunction(); // 声明 }; // 定义时无需static void MyClass::myStaticFunction() { /*...*/ } ``` 2. **调用方式** - 通过类名直接调用:`MyClass::myStaticFunction();` - 通过对象调用(不推荐):`obj.myStaticFunction();` [^1] #### 二、核心特性 1. **独立于实例** 静态函数不依赖类的实例,无法访问非静态成员变量或函数,也不能使用`this`指针。 ```cpp class MathUtils { public: static int add(int a, int b) { return a + b; } // 仅依赖参数,不访问成员变量 }; ``` 2. **访问权限限制** - 只能访问类的**静态成员变量**或其他**静态函数**: ```cpp class Counter { static int count; // 静态成员变量 public: static void increment() { count++; } // 允许访问静态成员 }; ``` - 若需操作非静态成员,需通过传递对象指针/引用(如回调函数场景)[^3]。 3. **作用域限定** 静态函数受类作用域约束,可通过`类名::函数名`调用,避免命名冲突。 #### 三、典型应用场景 1. **工具类函数** 实现与类相关但无需实例的通用功能,例如数学计算、类型转换等。 ```cpp class StringConverter { public: static std::string toUpper(const std::string& s); }; ``` 2. **单例模式** 通过静态函数控制唯一实例的获取: ```cpp class Singleton { private: static Singleton* instance; Singleton() {} public: static Singleton* getInstance() { if (!instance) instance = new Singleton(); return instance; } }; ``` 3. **回调函数封装** 静态函数可作为C风格回调的入口,再通过静态指针调用成员函数: ```cpp class Timer { static Timer* instance; // 保存this指针 public: Timer() { instance = this; } static void callback() { instance->handleTimeout(); } // 委托给成员函数 void handleTimeout() { /*...*/ } }; ``` #### 四、注意事项 1. **与`const`的冲突** 静态函数不能声明为`const`或`volatile`(因无`this`指针)。 2. **多线程安全** 若静态函数操作静态成员变量,需通过锁(如`std::mutex`)保证线程安全。 #### 五、完整示例 ```cpp #include <iostream> class DataProcessor { private: static int processedCount; // 静态成员变量 public: static void processData(int x) { std::cout << "Processing data: " << x << std::endl; processedCount++; // 操作静态变量 } static int getCount() { return processedCount; } }; int DataProcessor::processedCount = 0; // 静态变量初始化 int main() { DataProcessor::processData(42); // 直接通过类名调用 std::cout << "Total processed: " << DataProcessor::getCount(); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值