类方法表示 [英] class method representation

查看:69
本文介绍了类方法表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇C ++对象的结构。


类有方法和数据。

a类的不同实例将拥有自己的数据变量(是的,除非

我定义一个常见的变量,但让我们说我没有

那样做。)


我的问题是:每个实例都有它自己的

非静态方法的副本吗? br />

也许这是一个愚蠢的问题,但是它的动机是什么呢?

这个:


说我上课了使用一个非常庞大的复杂方法

我将实例化这个类中的许多对象。

如果每个实例都获得它自己的巨大方法代码副本,

然后我可以通过修改方法来节省一些内存

是一个函数(例如不是类方法)并且只有一个

它的副本。

I am curious about the structure of a C++ object.

Classes have methods, and data. Different instances of
a class will have their own data variables (yes, unless
I define a variable to be common, but let us say I did not
do that).

My question is this: does each instance have it''s own
copy of the non-static methods?

Maybe this is a dumb question, but what motivates it is
this:

Say I have a class with a really huge complicated method
and I will be instantiating many, many objects of this class.
If each instance gets it''s own copy of the huge method code,
then I might save some memory by modifying the method to
be a function (eg not a class method) and have only one
copy of it around.

推荐答案

emerth写道:
emerth wrote:
我很好奇C ++的结构对象。

类具有方法和数据。一个类的不同实例将拥有自己的数据变量(是的,除非我定义一个常见的变量,但让我们说我没有这样做)。

我的问题是:每个实例都有它自己的非静态方法的副本吗?
I am curious about the structure of a C++ object.

Classes have methods, and data. Different instances of
a class will have their own data variables (yes, unless
I define a variable to be common, but let us say I did not
do that).

My question is this: does each instance have it''s own
copy of the non-static methods?




通常不是。会发生的是每个方法都被转换(通过

编译器)到正常的方法。接受this的函数指向它操作的

对象实例。


例如,在这个例子中:


1 int i ;

2 std :: string s = ...;

3 char c = s.at(i);


编译器实际上会为第3行生成类似


char c = std :: string :: at(& s,i);


当然内联函数会使问题稍微复杂化,但即便如此,

你基本上得到每个函数调用的函数副本,而不是函数副本

函数每个对象。


但是亲自尝试一下;写两个具有相同数据成员的类和

不同的方法(数量和大小),并将它们的大小与

sizeof()运算符进行比较。



Usually not. What happens is that every method is converted (by the
compiler) to a "normal" function that accepts the "this" pointer to the
object instance it manipulates.

For instance, in this example:

1 int i;
2 std::string s = ...;
3 char c = s.at(i);

the compiler will actually generate for line 3 something like

char c = std::string::at(&s, i);

Of course inlining a function complicates matters slightly, but even then,
you basically get a copy of the function per function call, not a copy of
the function per object.

But try it yourself; write two classes with identical data members and
different methods (in number and "size") and compare their sizes with the
sizeof() operator.


emerth写道:
emerth wrote:
我很好奇C ++对象的结构。

类有方法,数据。一个类的不同实例将有自己的数据变量
(是的,除非我定义一个变量是常见的,但是让我们说我没有这样做)。

我的问题是:每个实例都有它自己的非静态方法的副本吗?

也许这是一个愚蠢的问题
但是什么激励它是这样的:

说我有一个非常庞大的复杂方法的类
我将实例化这个类的许多对象。
如果每个实例都得到它'自己的巨大方法代码的副本,
然后我可以通过修改方法作为一个函数(例如不是类方法)来保存一些内存
并且只有一个副本它周围。
I am curious about the structure of a C++ object.

Classes have methods, and data. Different instances of
a class will have their own data variables
(yes, unless I define a variable to be common,
but let us say I did not do that).

My question is this: does each instance have
it''s own copy of the non-static methods?

Maybe this is a dumb question
but what motivates it is this:

Say I have a class with a really huge complicated method
and I will be instantiating many, many objects of this class.
If each instance gets it''s own copy of the huge method code,
then I might save some memory
by modifying the method to be a function (e.g. not a class method)
and have only one copy of it around.




对象不*拥有,包含或包含方法。

不是C ++或任何其他面向对象的编程语言。

方法属于该类。如果有虚函数,

该对象将包含一个指向函数指针数组的指针

称为虚函数表。函数指针

指向与对象属于同一类的方法。



Objects do *not* have, include or contain methods.
Not in C++ or any other object oriented programming language.
The methods belong to the class. If there are virtual functions,
the object will contain a pointer to an array of function pointers
called the virtual function table. The function pointers
point to the methods which belong to the same class as the object.


emerth< em ** **@hotmail.com>在消息中写道

新闻:a9 ************************** @ posting.google.c om ...
emerth <em****@hotmail.com> wrote in message
news:a9**************************@posting.google.c om...
我很好奇C ++对象的结构。

类有方法和数据。一个类的不同实例将拥有自己的数据变量(是的,除非我定义一个常见的变量,但让我们说我没有这样做)。

我的问题是:每个实例都有它自己的非静态方法的副本吗?

也许这是一个愚蠢的问题,但是它的动机是什么呢?
这个:

说我有一个带有非常复杂的方法的类
我会实例化这个类的很多很多对象。
如果每个实例都得到了它是自己的巨大方法代码的副本,然后我可以通过修改方法来保存一些内存来成为一个函数(例如不是类方法)并且只有一个
副本它的周围。
I am curious about the structure of a C++ object.

Classes have methods, and data. Different instances of
a class will have their own data variables (yes, unless
I define a variable to be common, but let us say I did not
do that).

My question is this: does each instance have it''s own
copy of the non-static methods?

Maybe this is a dumb question, but what motivates it is
this:

Say I have a class with a really huge complicated method
and I will be instantiating many, many objects of this class.
If each instance gets it''s own copy of the huge method code,
then I might save some memory by modifying the method to
be a function (eg not a class method) and have only one
copy of it around.




出于语言规则的目的,最好将每个对象视为具有自己的对象
每个非静态数据和功能成员的副本。实际上,

就是你得到的。但是,在给定的可执行文件中,实际的

实现几乎肯定会在所有对象中共享相同的代码,

因此您不必担心代码增长每次你创建一个

对象。


DW



For the purposes of the language rules, it''s best to think of each object as
having its own copy of each non-static data and function member. In effect,
that''s what you get. However, in a given executable file, the actual
implementation will almost certainly share the same code among all objects,
so you don''t have to worry your code growing every time you create an
object.

DW


这篇关于类方法表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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