类的静态函数和类的构造函数之间有什么关系?

发布于 2024-11-01 18:03:05 字数 36 浏览 1 评论 0原文

每次在类上调用静态函数时会发生什么?构造函数什么时候执行?

What happens every time a static function is called on the class? When is the constructor executed?

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

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

发布评论

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

评论(6

胡渣熟男 2024-11-08 18:03:05

没有依赖关系,也就是说,您可以调用静态方法,而无需使用语法“Class::method()”构造实例

class someclass
{
public:
    someclass()
    {
        std::cout << "someclass constructor" << std::endl;
    }

    static void staticmethod()
    {
        std::cout << "someclass staticmethod" << std::endl;
    }
};

,然后

int main(int argc, char** argv)
{
    someclass::staticmethod();
    return 0;
}

您将看到打印出“someclass staticmethod”,而没有提及或不需要调用构造函数。

There is no dependency relationship, that is, you can call the static method without constructing an instance with the syntax "Class::method()"

class someclass
{
public:
    someclass()
    {
        std::cout << "someclass constructor" << std::endl;
    }

    static void staticmethod()
    {
        std::cout << "someclass staticmethod" << std::endl;
    }
};

and then

int main(int argc, char** argv)
{
    someclass::staticmethod();
    return 0;
}

you will see printed out "someclass staticmethod" with no mention or need for invoking the constructor.

Spring初心 2024-11-08 18:03:05

创建对象时会调用构造函数。

类的静态方法基本上是即时独立的。换句话说,该方法不保留单个对象的值,它们在所有对象之间共享

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

被翻牌 2024-11-08 18:03:05

静态成员函数的背景

static 成员函数可以随时调用,即使您还没有创建类的对象实例。当它们被调用时:

  • 它们不会像非静态成员函数那样接收隐式 this 指针
    • 因此,它们不会隐式自动感知是否存在该类的任何实例,更不用说实例在内存中的位置了。
  • 它们可以处理静态成员变量,这些变量同样独立于对象实例。

为了理解上述内容,可能有助于想象类或结构的静态成员与独立的非成员类似,除了:

  • 静态成员实际上与类有友谊
  • 出于查找目的,静态成员位于类的范围内匹配函数调用或查找变量静态成员的标识符
  • 可以在该范围内受到保护或私有,

因此,它们是非成员和成员行为的混合体。

对象构造如何与静态/非静态成员和线程相关

对象的构造函数可以利用类的静态成员,调用函数或使用变量。但是,请记住,静态成员变量就像单个全局变量,只不过它位于类的命名范围内:使用该变量的任何代码(无论该代码是静态或非静态成员函数还是非成员函数)都必须与其他代码使用该变量,因为该值是共享的。如果您编写多线程代码,则需要使用互斥锁或类似的工具来保护它,就像保护非成员变量一样。

静态成员变量的构造

我认为您可能想问一个关键问题:静态成员变量的构造函数何时被调用?根据标准 9.4.2(我在这里参考 C++98 最终草案):

-7- 静态数据成员的初始化和销毁​​与非本地对象(basic.start.init、basic.start.term)完全相同。

至关重要的是,最坏/最新情况的情况在 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:

  • they do not receive an implicit this pointer the way non-static member functions do
    • so, they're not implicitly automatically aware if there are any instances of the class, let alone where in memory instances are.
  • they can work on the static member variables, which are likewise independent of object instances.

To 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:

  • static members effectively have friendship with the class
  • static members are inside the class's scope for the purposes of looking up the identifiers to match function calls or find the variable
  • static members can be protected or private within that scope

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):

-7- Static data members are initialized and destroyed exactly like non-local objects (basic.start.init, basic.start.term).

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.*

几度春秋 2024-11-08 18:03:05

对于定义它的类来说,只有一份“静态”函数的副本......即该类的所有对象共享相同的“静态”函数。

仅当创建该类的对象时才会调用构造函数。对于 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.

荒路情人 2024-11-08 18:03:05

构造函数在对象实例化时执行。

The constructor is executed when an object is instantiated.

帥小哥 2024-11-08 18:03:05
  • 对于类方法,静态意味着可以在类本身上调用该方法,不需要该类的实例即可使用该方法
  • 对于基本 C/C++ 程序中的函数,静态函数是仅对 C/C++ 程序中的其他函数可见的函数同一个文件
  • For class methods, static means that this method can be called on the class itself, no instance of that class is necessary to use the method
  • For functions in a basic C/C++ program, static functions are functions that are only visible to other functions in the same file
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文