lundi 12 décembre 2022

Is it ok to call a static function which returns a local static object inside static function in c++?

Here is the code in c++.

class A {
private:
    A() {};
  void xx_func() {}
  void yy_func() {}
public:
  ~A() {};
  static A &GetInstance() {
    static A a_ins;
    return a_ins;
  }
  static void func1() {
      A::GetInstance().xx_func();
      // other code
  }
  static void func2() {
      A::GetInstance().yy_func();
      //other code
  }
};

The GetInstance function return a local static variable. Is it safe for func1func2 to call A::GetInstance()?

Aucun commentaire:

Enregistrer un commentaire