内存中静态方法和非静态方法的区别 [英] Difference between static method and non static function in memory

查看:360
本文介绍了内存中静态方法和非静态方法的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



静态函数用于访问静态成员。然而,静态函数在其应用的寿命期间也仅存在一个。除了作为静态访问器,在低级别是不同于正常的类函数,不是吗?或者也许我错了,每个类都有自己的功能?

解决方案

这在不同语言之间是不同的,但在C或C ++ 03函数中通常映射汇编函数;即它们存在于内存中(无论是自由函数,类函数还是类静态函数),并接受参数作为参数,包括隐式成员函数的 this 指针。 / p>

在C ++ 11中,lambda函数引入了一个新颖性:所谓的函数的每个实例都会带有一些状态。从实现的角度来看,因此意味着需要创建常规函数,并将其与匿名数据包相关联(如果需要)。每次创建lambda时,函数不需要重复,而是数据。一个有用的图是记住lambdas(在C ++中)替换函数对象(或谓词对象):它们只是语法糖,实现是类似的。


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?

解决方案

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.

这篇关于内存中静态方法和非静态方法的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆