静态方法和非静态函数在内存中的区别

发布于 2024-12-08 00:45:02 字数 210 浏览 0 评论 0原文

据我了解,类的每个实例在内存中都有自己的成员变量,这样它就可以为不同的对象存储不同的值。然而,对于成员函数则不一样。成员函数在类的对象之间重用,因此它只有一个地址和一块内存,以便在所有对象需要时引用。

静态函数用于访问静态成员。然而,静态函数在其应用程序的生命周期内也只存在一个。除了作为静态访问器之外,在低级别上它与普通的类函数没有什么不同,不是吗?或者也许我错了,每个类都有自己的功能?

As I understand, each instance of a class has its own member variables in memory, so that it can store different values for different objects. However, it is not the same for member functions. Member functions are reused across objects of a class, so it only has one address with one block of memory to refer when needed by all objects.

Static function is made to access static members. However, static function also exists only one during the lifetime of its application. Aside from being the static accessor, at low level it is not different with normal class functions, isn't it? Or maybe I'm wrong, that each class has its own functions?

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

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

发布评论

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

评论(4

烟火散人牵绊 2024-12-15 00:45:02

非静态函数接受附加参数 this,它是指向具有特定于实例的变量的类实例的指针。

静态函数没有 this 参数(因此不能在静态函数中使用 this ,只能访问静态数据成员)。

Non-static functions accept additional parameter, this, which is the pointer to the class instance with the instance-specific variables.

Static functions don't have this parameter (thus you can't use this in a static function and can only access static data members).

潜移默化 2024-12-15 00:45:02

这因语言而异,但在 C 或 C++03 中,函数通常映射到汇编函数;也就是说,它们在内存中存在一次(无论是自由函数、类函数还是类静态函数),并采用参数作为参数,包括隐式成员函数的 this 指针。

在 C++11 中,lambda 函数引入了一个新奇之处:所谓函数的每个实例都将携带一些状态。从实现的角度来看,这意味着需要创建一个“常规”函数并将其与匿名数据包相关联(如果需要)。每次创建 lambda 时,不需要复制该函数,但需要复制数据。一个有用的数字是记住 lambda(在 C++ 中)取代了函数对象(或谓词对象):它们只是语法糖,实现是相似的。

This differs from language to language, but in C or C++03 functions generally map on assembly functions; that is they exist once in memory (whether free functions, class functions or class static functions) and take arguments as parameters, including a this pointer for member functions that is implicit.

In C++11, lambda functions introduce a novelty: each instance of the so-called function will carry some state. From an implementation point of view, it therefore means that a "regular" function needs be created and it is associated to an anonymous bundle of data (if necessary). The function need not be duplicated each time the lambda is created, but the data does. One helpful figure is to remember that lambdas (in C++) replace function objects (or predicate objects): they are just syntactic sugar, the implementation is similar.

郁金香雨 2024-12-15 00:45:02

静态函数和成员函数之间的唯一区别是成员函数始终自动传入 this 指针。

The only difference between static and member functions is that member functions always have the this pointer passed in automatically.

老街孤人 2024-12-15 00:45:02

简而言之,如果引用静态函数,静态函数会为其自身创建一组内存,并且适用于通常不可更改的静态数据成员。但非静态函数为每个实例创建单独的内存集,并且适用于非静态和静态数据成员。如果您需要稳定的输出,请选择静态,如果您需要备用输出,请选择非静态。

simply if it is referred, static functions creates a single set of memory for itself and are meant for static data-members which are generally not changeable. But non-static functions creates separate set of memories for each instances and are meant for both non-static and static data-members. If u require stable output go for static and if u require the alternate go for the non-static.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文