物体的方法 [英] Methods of Objects

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

问题描述

类的方法是否只为所有对象或每个对象创建一次

作为其方法的副本?


似乎我只能使用::访问一个对象方法的地址所以如果我

有一个带有方法func的类,我想要它的地址我会做

& someClass: :someFunc似乎暗示每个类只有一个函数

(因此对于该类的所有实例化)...当我创建一个

非常大的数组时这个类的对象似乎支持这个,因为

文件大小没有太大变化...


我只是想确定那里没有必要处理静态成员和

的东西,以确保我优化大小,如果编译器自动处理

(我希望它)?即这两个班级之间几乎没有差别



A级

{

int x;

public:

void func(int y){x = y; }

}





B级

{

int x;

public:

static void func(B& b,int y){bx = y; }

}

a.func(1);





b.func(b,1);


应该生成完全相同的代码吗? (或非常非常接近?)我希望编译器内部将A类作为B类处理,或者甚至更好。

的原因是,即使我做了像B这样的事情,B类只有1个函数。
b [10000];虽然我不确定A级..如果我做一个[10000]如果它

创建了10000个func副本。虽然我没有看到任何理由为什么会发生这种情况以及我所有的测试。说它不...我只想确保

如此。


谢谢,

Jon

Are methods of a class created only once for all objects or for each object
as its own copy of its methods?

It seems that I can only access an object method''s address using :: so if I
have a class with method func and I want its address I would do
&someClass::someFunc which seems to imply that there is only one function
per class(and hence for all instantiations of that class)... when I create a
very large array of objects of that class it seems to back this up as the
file size does not change much...

I just want to be sure that there is no need to deal with static members and
stuff for no to make sure I optimize size if the compiler handles it
automatically(which I hope it does)? i.e. there is virtually no difference
between these two classes
class A
{
int x;
public:
void func(int y) { x = y; }
}

and

class B
{
int x;
public:
static void func(B &b, int y) { b.x = y; }
}
a.func(1);

and

b.func(b, 1);

should produce the exact same code? (or very very close?) I''m hoping that
the compiler internally handles class A as class B or maybe even better. The
reason is that simply class B has only 1 func even if I do something like B
b[10000]; while I''m not sure about class A.. if I do A a[10000] if it
creates 10000 copies of func or not. Though I don''t see any reason why this
would happen and all my "tests" say it doesn''t... I just want to make sure
so.

Thanks,
Jon

推荐答案

Jon Slaughter写道:
Jon Slaughter wrote:
类的方法只为所有对象或每个对象创建一次
作为自己的方法副本?


前者。


[snip]

我只想确保没有必要如果编译器自动处理它(我希望它会这样做),处理静态成员和
的东西是否可以确保我优化大小?即这两个班级之间几乎没有差异

A级
{
int x;
公众:
void func(int y){x = y; }



B类
{
int x;
公开:
静态虚函数(B& b,int y){bx = y; }
}


上面的含义是sizeof(A)== sizeof(B)== sizeof(int)。即,每个

类应该是int的大小。

a.func(1);



b.func(b,1);

应该生成完全相同的代码吗? (或者非常非常接近?)


我希望它们会非常接近,但这没关系 - 会员的副本数量

函数不会随着对象的增加而增加

构造。

我希望编译器内部将A类作为B类处理,或者甚至更好。原因是,即使我做了类似B的事情,B级也只有1个功能[10000];虽然我不确定A级..如果我做了一个[10000],如果它创造了10000份func或者没有。
Are methods of a class created only once for all objects or for each object
as its own copy of its methods?
The former.

[snip]
I just want to be sure that there is no need to deal with static members and
stuff for no to make sure I optimize size if the compiler handles it
automatically(which I hope it does)? i.e. there is virtually no difference
between these two classes
class A
{
int x;
public:
void func(int y) { x = y; }
}

and

class B
{
int x;
public:
static void func(B &b, int y) { b.x = y; }
}
The above implies sizeof(A) == sizeof(B) == sizeof(int). I.e., each
class should be the size of an int.
a.func(1);

and

b.func(b, 1);

should produce the exact same code? (or very very close?)
I expect they would be quite close, but it doesn''t matter - the number
of copies of the member function does not increase as objects are
constructed.
I''m hoping that
the compiler internally handles class A as class B or maybe even better. The
reason is that simply class B has only 1 func even if I do something like B
b[10000]; while I''m not sure about class A.. if I do A a[10000] if it
creates 10000 copies of func or not.




这不是。


祝你好运,


Tom



It doesn''t.

Best regards,

Tom




Thomas Tutone写道:

Thomas Tutone wrote:
Jon Slaughter写道:
Jon Slaughter wrote:
类的方法只为所有对象或每个对象创建一次
作为它自己的方法副本?
Are methods of a class created only once for all objects or for each object
as its own copy of its methods?



前者。



The former.




我应该限定这个,我想。类'的成员

函数的代码不会影响类的大小,因为代码是

而不是数据。但是,如果您定义成员函数内联(在您的示例中为/ b
),那么如果编译器符合您的内联

请求,则该代码的新副本每次调用

成员函数时都会插入。但它仍然与

构造对象的大小无关,并且每个对象都没有它自己的副本

。会员功能。


祝你好运,

Tom



I should qualify this, I suppose. The code for the class''s member
functions does not affect the size of the class, because the code is
not data. However, if you define the member function inline (as you do
in your example), then if the compiler complies with your inlining
request, a new copy of that code is inserted every time you call that
member function. It still has nothing to do with the size of the
constructed object, though, and each object does not have "its own copy
of it" member functions.

Best regards,

Tom


Jon Slaughter写道:
Jon Slaughter wrote:
类的方法只为所有对象或每个对象创建一次
作为其方法的副本吗?

一次,对象的实例由this指针标识。

似乎我只能使用::访问对象方法的地址,所以如果我有一个带有方法函数的类,我想要它的地址我会做
&someClass :: someFunc这似乎意味着每个类只有一个函数(因此对于该类的所有实例化)...当我创建一个
该类的非常大的一系列对象似乎支持这个,因为
文件大小没有太大变化...

我只想确定有没有必要处理静态成员和
的东西,以确保我优化大小如果编译器自动处理它(其中我希望如此)?即这两个类之间几乎没有区别
Are methods of a class created only once for all objects or for each object
as its own copy of its methods?
Once, the instance of the object is identified by the this pointer.
It seems that I can only access an object method''s address using :: so if I
have a class with method func and I want its address I would do
&someClass::someFunc which seems to imply that there is only one function
per class(and hence for all instantiations of that class)... when I create a
very large array of objects of that class it seems to back this up as the
file size does not change much...

I just want to be sure that there is no need to deal with static members and
stuff for no to make sure I optimize size if the compiler handles it
automatically(which I hope it does)? i.e. there is virtually no difference
between these two classes



注意,指向成员函数的指针是一个与

指针不同的指针一个静态成员。


Ian


Note that a pointer to a member function is a different beast from a
pointer to a static member.

Ian


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

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