类的静态函数和类的构造函数之间有什么关系?
每次在类上调用静态函数时会发生什么?构造函数什么时候执行?
What happens every time a static function is called on the class? When is the constructor executed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
没有依赖关系,也就是说,您可以调用静态方法,而无需使用语法“Class::method()”构造实例
,然后
您将看到打印出“someclass staticmethod”,而没有提及或不需要调用构造函数。
There is no dependency relationship, that is, you can call the static method without constructing an instance with the syntax "Class::method()"
and then
you will see printed out "someclass staticmethod" with no mention or need for invoking the constructor.
创建对象时会调用构造函数。
类的静态方法基本上是即时独立的。换句话说,该方法不保留单个对象的值,它们在所有对象之间共享
Constructors are called when the object is created.
static methods of a class are basically instant independent. In other words, the method does not keep values for individual objects they are shared amongst all the objects
静态成员函数的背景
static
成员函数可以随时调用,即使您还没有创建类的对象实例。当它们被调用时:this
指针为了理解上述内容,可能有助于想象类或结构的静态成员与独立的非成员类似,除了:
因此,它们是非成员和成员行为的混合体。
对象构造如何与静态/非静态成员和线程相关
对象的构造函数可以利用类的静态成员,调用函数或使用变量。但是,请记住,静态成员变量就像单个全局变量,只不过它位于类的命名范围内:使用该变量的任何代码(无论该代码是静态或非静态成员函数还是非成员函数)都必须与其他代码使用该变量,因为该值是共享的。如果您编写多线程代码,则需要使用互斥锁或类似的工具来保护它,就像保护非成员变量一样。
静态成员变量的构造
我认为您可能想问一个关键问题:静态成员变量的构造函数何时被调用?根据标准 9.4.2(我在这里参考 C++98 最终草案):
至关重要的是,最坏/最新情况的情况在 3.6.2 中进行了描述:
-3- 是否动态初始化(dcl.init、class.static、class.ctor、class.expl.init)是实现定义的命名空间范围的对象在 main 的第一条语句之前完成。如果初始化被推迟到 main 的第一个语句之后的某个时间点,则它应在首次使用与要初始化的对象相同的翻译单元中定义的任何函数或对象之前发生。*
Background on static member functions
static
member functions can be called at any time, even if you haven't created an object instance of the class. When they're called:this
pointer the way non-static member functions doTo understand the above, it may help to imagine that the static members of a class or struct are similar to freestanding non-members except that:
So, they're a hybrid of non-member and member behaviours.
How object construction relates to static/non-static members and threading
The constructor of objects can utilise static members of the class, either calling functions or using variables. But, remember that the static member variable is like a single global variable except that it's in the class's naming scope: any code using the variable - whether that code's a static or non-static member function or a non-member function - must cooperate with other code using the variable because the value is shared. If you write multi-threaded code, you'll need to use a mutex or similar to protect it exactly as you would for a non-member variable.
Construction of static member variables
There is one crucial issue that I think you may intend by the question: when is the constructor for the static member variables called? According to the Standard 9.4.2 (I'm referencing the C++98 final draft here):
Crucially, the worst/latest-case scenario is described in 3.6.2:
-3- It is implementation-defined whether or not the dynamic initialization (dcl.init, class.static, class.ctor, class.expl.init) of an object of namespace scope is done before the first statement of main. If the initialization is deferred to some point in time after the first statement of main, it shall occur before the first use of any function or object defined in the same translation unit as the object to be initialized.*
对于定义它的类来说,只有一份“静态”函数的副本......即该类的所有对象共享相同的“静态”函数。
仅当创建该类的对象时才会调用构造函数。对于 C++,当您使用 new 运算符创建该类的对象时,将调用构造函数。
要调用静态方法,您需要使用范围解析运算符 (::) 并使用类名限定方法名称...但是,对于构造函数,它会使用“new”运算符自动调用。
希望这有帮助。
There is only one copy of the 'static' function for the class in which it is defined... i.e. all the objects of that class share the same 'static' function.
The constructor is only called when the object of that class is created. In case of c++, the constructor is called when you create an object of that class with the
new
operator.To call a static method you need to use the Scope Resolution operator (::) and qualify the method name with the class name... However, in case of the constructor, it gets automatically invoked with the "new" operator.
Hope this helps.
构造函数在对象实例化时执行。
The constructor is executed when an object is instantiated.