可以从静态函数中调用构造函数吗?
如果以前有人问过这个问题,我很抱歉(我试图找到它但徒劳)。 我正在经历单例设计模式 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以从任何地方调用构造函数。如果没有实例就无法调用构造函数,那么首先如何获取实例?
您可能已经无数次从普通函数中调用它们。静态函数也不例外。
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.
是的,可以从静态函数调用构造函数。每当创建新对象时,编译器都会自动调用构造函数。它有助于类成员的正确初始化。是的,在某种程度上,您可以将构造函数视为可以从静态函数调用的特殊函数。
在上面的代码中,您正在实现一种称为
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
.第1点:在C++中,不能在静态成员函数内调用非静态成员函数。原因是静态函数中没有“this”指针。 构造函数也不例外。
第 2 点:您始终可以调用不同对象的方法,即使它是非静态的!
结论
它与代码中的构造函数无关。因为你正在创建另一个对象。您可以调用其所有公共方法,包括其构造函数。
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.
Point 2: You can always call the methods of a different object, even if it's non-static!
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.
是的,
new ClassName
-->它将Yes,
new ClassName
--> it will call the constructor (on a new instance...)