方法:

给类写一个静态函数

static myClass* instance()
{
        static myClass ins;
        return &ins;
};

采用如下方式得到一个myClass的实例:

myClass::instance();

问题:

众所周知,函数体内局部变量的生命周期是函数的存在期。在instance()消亡以后,内存中仍然存在一个static的myCalss实例ins。这在逻辑上存在矛盾。

正因为这种矛盾,C#中废止了这种函数体内的static变量。

相关文章:

  • 2021-09-29
  • 2021-10-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-07
猜你喜欢
  • 2022-01-01
  • 2021-12-15
  • 2022-01-18
  • 2022-12-23
  • 2021-10-18
  • 2021-06-24
  • 2022-12-23
相关资源
相似解决方案