可以从静态函数中调用构造函数吗?

发布于 2024-11-04 16:45:19 字数 1451 浏览 1 评论 0原文

如果以前有人问过这个问题,我很抱歉(我试图找到它但徒劳)。 我正在经历单例设计模式 http://sourcemaking.com/design_patterns/singleton/cpp/1 (代码是从那里复制的)

class GlobalClass
{
    int m_value;
    static GlobalClass *s_instance;
    GlobalClass(int v = 0)
    {
        m_value = v;
    }
  public:
    int get_value()
    {
        return m_value;
    }
    void set_value(int v)
    {
        m_value = v;
    }
    static GlobalClass *instance()
    {
        if (!s_instance)
          s_instance = new GlobalClass;
        return s_instance;
    }
};

// Allocating and initializing GlobalClass's
// static data member.  The pointer is being
// allocated - not the object inself.
GlobalClass *GlobalClass::s_instance = 0;

void foo(void)
{
  GlobalClass::instance()->set_value(1);
  cout << "foo: global_ptr is " << GlobalClass::instance()->get_value() << '\n';
}

void bar(void)
{
  GlobalClass::instance()->set_value(2);
  cout << "bar: global_ptr is " << GlobalClass::instance()->get_value() << '\n';
}

int main()
{
  cout << "main: global_ptr is " << GlobalClass::instance()->get_value() << '\n';
  foo();
  bar();
}

我的问题在行中 if (!s_instance) s_instance = 新的全局类;

s_instance = new GlobalClass 将调用构造函数,但构造函数是非静态的,我们从静态函数调用它。这是如何运作的?构造函数在这方面是否“特殊”?

谢谢!

I am sorry if this question has been asked before (I tried to find it but in vain).
I was going through Singleton design pattern at http://sourcemaking.com/design_patterns/singleton/cpp/1 (code is copied from there)

class GlobalClass
{
    int m_value;
    static GlobalClass *s_instance;
    GlobalClass(int v = 0)
    {
        m_value = v;
    }
  public:
    int get_value()
    {
        return m_value;
    }
    void set_value(int v)
    {
        m_value = v;
    }
    static GlobalClass *instance()
    {
        if (!s_instance)
          s_instance = new GlobalClass;
        return s_instance;
    }
};

// Allocating and initializing GlobalClass's
// static data member.  The pointer is being
// allocated - not the object inself.
GlobalClass *GlobalClass::s_instance = 0;

void foo(void)
{
  GlobalClass::instance()->set_value(1);
  cout << "foo: global_ptr is " << GlobalClass::instance()->get_value() << '\n';
}

void bar(void)
{
  GlobalClass::instance()->set_value(2);
  cout << "bar: global_ptr is " << GlobalClass::instance()->get_value() << '\n';
}

int main()
{
  cout << "main: global_ptr is " << GlobalClass::instance()->get_value() << '\n';
  foo();
  bar();
}

My question is in the lines
if (!s_instance)
s_instance = new GlobalClass;

s_instance = new GlobalClass will call the constructor but constructor is non-static and we are calling it from a static function. How does that work? Is Constructor "special" in this regard?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦罢 2024-11-11 16:45:19

您可以从任何地方调用构造函数。如果没有实例就无法调用构造函数,那么首先如何获取实例?

您可能已经无数次从普通函数中调用它们。静态函数也不例外。

You can call a constructor from anywhere. If you couldn't call a constructor without an instance, how would you get any instances in the first place?

You've probably called them from plain functions countless times. Static functions are no different.

枕梦 2024-11-11 16:45:19

是的,可以从静态函数调用构造函数。每当创建新对象时,编译器都会自动调用构造函数。它有助于类成员的正确初始化。是的,在某种程度上,您可以将构造函数视为可以从静态函数调用的特殊函数。

在上面的代码中,您正在实现一种称为 Singleton 的设计模式。

Yes constructor can be called from a static function. Constructor is automatically called by the compiler whenever a new object is created. It facilitates for proper initialization of the class members. Yes In a way you can consider Constructor as a special function which can be called from a static function.

In your code above, You are implementing a design pattern called as Singleton.

娇俏 2024-11-11 16:45:19

第1点:在C++中,不能在静态成员函数内调用非静态成员函数。原因是静态函数中没有“this”指针构造函数也不例外

class A
{
public:
    void fun(){};
    static void static_fun()
    {
        // This is illegal because fun() is non-static
        fun();
    }
};

第 2 点:您始终可以调用不同对象的方法,即使它是非静态的

class A
{
public:
    void fun(){};
    static void static_fun(A& a)
    {
        // Legal here because it's a (different) object..
        a.fun();
    }
};

结论

它与代码中的构造函数无关。因为你正在创建另一个对象。您可以调用其所有公共方法,包括其构造函数。

Point 1: In C++, you cannot call a non-static member function inside a static member function. The reason is that there is no "this" pointer in static function. Constructors are no exception.

class A
{
public:
    void fun(){};
    static void static_fun()
    {
        // This is illegal because fun() is non-static
        fun();
    }
};

Point 2: You can always call the methods of a different object, even if it's non-static!

class A
{
public:
    void fun(){};
    static void static_fun(A& a)
    {
        // Legal here because it's a (different) object..
        a.fun();
    }
};

Conclusion

It has nothing to do with constructor here in your code. Because you are creating another object. You can call all its public methods including its constructor.

梦醒时光 2024-11-11 16:45:19

是的,

  • 很简单:使用 new ClassName -->它将
  • 非常重要地调用构造函数(在新实例上...): placement new (仅适用于非常明确的库实现类型、场景)

Yes,

  • trivially: use new ClassName --> it will call the constructor (on a new instance...)
  • non-trivially: placement new (useful only for very explicit, library implementation type, scenarios)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文