关于阶级和对象的怀疑 [英] Doubts regarding class and object

查看:74
本文介绍了关于阶级和对象的怀疑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我知道这些功能只在内存中加载一次。我怀疑是......如果我们创建一个类的对象如何引用该函数?对象创建和函数调用背后的东西是什么?该函数的对象中是否有指针?

Hi All,

I know the functions are loaded only once in the memory. My doubt is... if we create an object of a class how it refer the function? What is the thing behind the object creation and function calling? Is there any pointer in the object to the function?

推荐答案

如果声明类或结构的方法,则只创建实现此方法的一段代码,每个实例一个。为什么?嗯...只是因为OOP不是由白痴创造的。 :-)



现在,问题是:在相同调用的不同实例上调用的相同方法显然会执行不同的工作,因为它适用于不同的实例。问题很简单:它与静态方法完全相同,只有一个简单的区别:它有一个额外的参数,它被隐式传递给一个方法;并且此参数是对该实例的引用。猜猜这是什么?这是一个参数this。



如需更多解释,请参阅我过去的解决方案:是什么让静态方法可以访问? [ ^ ]。



另一个有趣的事实:。 NET代码是JIT编译的。当代码编译为本机CPU指令?实际上,它是按照方法基本编译的。如果某个方法链接到程序集但从未调用过,则此方法在整个运行时间内永远不会进行JIT编译。







阅读本文,了解有关在场景后面创建运行时对象的详细信息:http://msdn.microsoft.com/en-us/magazine/cc163791.aspx [ ^ ]。



-SA
[我不知道为什么下面会出现如此多的空白区域 - SA]
If you declare a method of a class or a structure, only one piece of code implementing this method is created, one per all instances. Why? Well… just because OOP wasn''t created by idiots. :-)

Now, the question is: the same method called on different instances of the same calls apparently does different job, well, because it works with different instance. The question is simple: it works exactly as a static methods, with one simple difference: it has one extra parameter which is implicitly passed to a method; and this parameter is a reference to that very instance. And guess what is this? This is a parameter "this".

For some more explanations, please see my past solution: What makes static methods accessible?[^].

One more interesting fact: .NET code is JIT-compiled. When the code is compiled to the native CPU instructions? In fact, it is compiled on demand on per-method basic. If some method are linked into the assembly but never called, this method will never be JIT-compiled during the whole run time.



Read this article for in-depth information on creating the run-time objects behind the scene: http://msdn.microsoft.com/en-us/magazine/cc163791.aspx[^].

—SA
[I have no idea why so much white space is rendered below — SA]


除了SA'的答案之外,每个类只需要一个版本的原因是内存映射。

变量是每个实例,它是每个类的功能步骤。如果方法引用 this.foo ,那么代码将查看堆中当前对象的内存 this ],并检索 foo 的项目。另一个例子,代码是相同的,只是使用的内存不同。
Further to SA''s answer, the reason only one version of each class is needed is down to the memory map.
The variables are per-instance, it is the functional steps that are per-class. If a method references this.foo, then the code will look at the memory in the heap for the current object this], and retrieve the item for foo. For another instance, the code is the same, just the memory being used is different.


Hi,

Here is resume on programs memory structure :

-Stack : It is used for holding the return address at function calls, arguments passed and local variable for functions; it also stores the current state of the CPU.
-Heap : Memory area is a region of free memory from where memory is allocated for the dynamic allocation in C++.
-Global Variable : This space is used to store the global variable used in the program.
-Program Code : This region holds the compiled code of the program.

For member functions, the code for those is not different from free-function code in terms of where it goes in the executable image : "Program Code" Area.

After all, a member function is basically a free function with an implicit "this" pointer as its first argument.


When an object is allocating with "new", a new instance of members vars are allocated in the Heap but a member function code is associated to the class and stored just one time in "Program Code" Area.







这是我对已发布问题的解决方案存储了哪些功能 [ ^ ]


这篇关于关于阶级和对象的怀疑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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