物体的方法

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

问题描述

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

作为其方法的副本?


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

有一个带有方法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

推荐答案




这不是。


祝你好运,


Tom



It doesn''t.

Best regards,

Tom




前者。



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




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

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


Ian


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

Ian


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

08-14 15:00
查看更多