POD结构如何与成员函数相关联? [英] How are POD structs associated with member functions?

查看:49
本文介绍了POD结构如何与成员函数相关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有虚函数的类的实现在概念上很容易理解:它们使用vtable。这引出了关于POD结构的问题:如何将

与它们在常见实现中的成员函数相关联?

这个''''ptr隐藏在哪里? POD结构与会员

功能?


John

解决方案

< blockquote> JohnQ写道:


具有虚函数的类的实现在概念上很容易理解:它们使用vtable。这引出了关于

POD结构的问题:它们如何与其成员函数相关联?
常见实现?对于具有成员函数的POD结构,''this''ptr在哪里隐藏在




是的,''this''指针通常会传递给函数,因为

隐藏了zeroth论证。


V

-

请在通过电子邮件回复时删除资金''A' />
我没有回复最热门的回复,请不要问




" Victor Bazarov" < v。******** @ comAcast.netwrote in message

news:f5 ********** @ news.datemas.de ...


JohnQ写道:


>具有虚函数的类的实现在概念上很容易明白:他们使用vtables。这引出了关于POD结构的问题:它们如何与常见实现中的成员函数相关联?对于具有成员函数的POD结构,''this''ptr在哪里隐藏?



是的,''this''指针通常会传递给函数,因为

隐藏了zeroth论点。



问题是这些函数是如何与结构静态关联的。

所以在RAM中有这个POD结构,例如,一个成员函数得到了''

。 POD-struct是数据成员的大小,所以

中没有任何内容显然指向函数。点或运算符如何使用
访问成员函数?某种特殊的编译器只能重载

的成员访问运算符,可以区分数据成员和

成员函数访问权限吗?


John


6月23日,11:19,JohnQ < johnqREMOVETHISprogram ... @ yahoo.comwrote:


" Victor Bazarov" < v.Abaza ... @ comAcast.netwrote in message


news:f5 ********** @ news.datemas.de ...


JohnQ写道:


具有虚函数的类的实现在概念上是

容易理解:他们使用vtables。这引出了关于

POD结构的问题:它们如何与其成员函数相关联?
常见实现?对于具有成员函数的POD结构,''this''ptr在哪里隐藏在



是的,''this''指针通常会传递给函数,因为

隐藏了"零"论点。



问题是这些函数是如何与结构静态关联的。

所以在RAM中有这个POD结构,例如,一个成员函数得到了''

。 POD-struct是数据成员的大小,所以

中没有任何内容显然指向函数。



为什么需要?


显然*它实际上是如何工作的是一个实现细节。所有

重要的是表现出所需的行为。但是我会按照这样的方式来思考:


你写的是


struct foo
{

void func(){i = 42; }


int i;

};


int main()

{

foo a_foo;

a_foo.func();


foo another_foo;

another_foo .func();


foo a_third_foo;

a_third_foo.i = 42;

}

编译器按照


struct foo

{

int i;

};

void foo_func(foo * this){this-> i = 42; }


int main()

{

foo a_foo;

foo_func(& a_foo) );


foo another_foo;

foo_func(& another_foo);


foo a_third_foo;

a_third_foo.i = 42;

}


我不是建议编译器实际重写你的代码看看

就是这样 - 只是说明了可能发生的事情。


点或运营商怎样才能获得
会员职能?某种特殊的编译器只能重载

的成员访问运算符,可以区分数据成员和

成员函数访问?



鉴于上面的struct foo,编译器知道哪些成员是
函数(func),哪些是数据成员(i)。当我这样做时,我需要一个b $ b a_third_foo.i = 42;


它知道我想要使用哪个foo对象(a_third_foo),它知道

其中该对象在内存中(无论它放在哪里)并且它知道

其中,在该内存中,int i是我想要设置为42的。 />

当我这样做时


another_foo.func();


它知道我想打电话一个函数(func),它知道

的代码在哪里(无论它放在哪里)它都知道哪个对象为
传递隐藏的地址"这"参数(another_foo)。


Gavin Deane


The implementation of classes with virtual functions is conceptually easy to
understand: they use vtables. Which begs the question about POD structs: how
are they associated with their member functions in common implementations?
And where is the ''this'' ptr tucked away at for POD structs with member
functions?

John

解决方案

JohnQ wrote:

The implementation of classes with virtual functions is conceptually
easy to understand: they use vtables. Which begs the question about
POD structs: how are they associated with their member functions in
common implementations? And where is the ''this'' ptr tucked away at
for POD structs with member functions?

Yes, the ''this'' pointer is usually passed into the function as the
hidden "zeroth" argument.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask



"Victor Bazarov" <v.********@comAcast.netwrote in message
news:f5**********@news.datemas.de...

JohnQ wrote:

>The implementation of classes with virtual functions is conceptually
easy to understand: they use vtables. Which begs the question about
POD structs: how are they associated with their member functions in
common implementations? And where is the ''this'' ptr tucked away at
for POD structs with member functions?


Yes, the ''this'' pointer is usually passed into the function as the
hidden "zeroth" argument.

The question is how the functions are staticly associated with the struct.
So there''s this POD-struct in RAM, for example, and a member function get''s
called. The POD-struct is the size of the data members so there''s nothing in
it pointing to the functions apparently. How can the dot or -operators
access the member functions? Some kind of special compiler-only overloading
of the member access operators that can distinguish between data member and
member function access?

John


On 23 Jun, 11:19, "JohnQ" <johnqREMOVETHISprogram...@yahoo.comwrote:

"Victor Bazarov" <v.Abaza...@comAcast.netwrote in message

news:f5**********@news.datemas.de...

JohnQ wrote:

The implementation of classes with virtual functions is conceptually
easy to understand: they use vtables. Which begs the question about
POD structs: how are they associated with their member functions in
common implementations? And where is the ''this'' ptr tucked away at
for POD structs with member functions?

Yes, the ''this'' pointer is usually passed into the function as the
hidden "zeroth" argument.


The question is how the functions are staticly associated with the struct.
So there''s this POD-struct in RAM, for example, and a member function get''s
called. The POD-struct is the size of the data members so there''s nothing in
it pointing to the functions apparently.

Why would there need to be?

Obviously *how* it actually works is an implementation detail. All
that matters is that the required behaviour is exhibited. But I would
think something along the lines of this:

You write

struct foo
{
void func() { i = 42; }

int i;
};

int main()
{
foo a_foo;
a_foo.func();

foo another_foo;
another_foo.func();

foo a_third_foo;
a_third_foo.i = 42;
}

The compiler treats it along the lines of

struct foo
{
int i;
};
void foo_func(foo* this) { this->i = 42; }

int main()
{
foo a_foo;
foo_func(&a_foo);

foo another_foo;
foo_func(&another_foo);

foo a_third_foo;
a_third_foo.i = 42;
}

I''m not suggesting the compiler actually rewrites your code to look
like that - just illustrating what''s probably going on.

How can the dot or -operators
access the member functions? Some kind of special compiler-only overloading
of the member access operators that can distinguish between data member and
member function access?

Given my struct foo above, the compiler knows which members are
functions (func) and which are data members (i). When I do

a_third_foo.i = 42;

it knows which foo object I want to work with (a_third_foo), it knows
where that object is in memory (wherever it put it) and it knows
where, within that memory, the int i is that I want to set to 42.

And when I do

another_foo.func();

it knows I want to call a function (func), it knows where the code for
that function is (wherever it put it) and it knows which object to
pass the address of as the hidden "this" parameter (another_foo).

Gavin Deane


这篇关于POD结构如何与成员函数相关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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