编译器按照 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 tounderstand: they use vtables. Which begs the question about POD structs: howare they associated with their member functions in common implementations?And where is the ''this'' ptr tucked away at for POD structs with memberfunctions?John 解决方案 JohnQ wrote:The implementation of classes with virtual functions is conceptuallyeasy to understand: they use vtables. Which begs the question aboutPOD structs: how are they associated with their member functions incommon implementations? And where is the ''this'' ptr tucked away atfor POD structs with member functions?Yes, the ''this'' pointer is usually passed into the function as thehidden "zeroth" argument.V--Please remove capital ''A''s when replying by e-mailI do not respond to top-posted replies, please don''t ask"Victor Bazarov" <v.********@comAcast.netwrote in messagenews:f5**********@news.datemas.de...JohnQ wrote:>The implementation of classes with virtual functions is conceptuallyeasy to understand: they use vtables. Which begs the question aboutPOD structs: how are they associated with their member functions incommon implementations? And where is the ''this'' ptr tucked away atfor POD structs with member functions?Yes, the ''this'' pointer is usually passed into the function as thehidden "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''scalled. The POD-struct is the size of the data members so there''s nothing init pointing to the functions apparently. How can the dot or -operatorsaccess the member functions? Some kind of special compiler-only overloadingof the member access operators that can distinguish between data member andmember function access?John On 23 Jun, 11:19, "JohnQ" <[email protected]:"Victor Bazarov" <[email protected] in messagenews:f5**********@news.datemas.de... JohnQ wrote:The implementation of classes with virtual functions is conceptuallyeasy to understand: they use vtables. Which begs the question aboutPOD structs: how are they associated with their member functions incommon implementations? And where is the ''this'' ptr tucked away atfor 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''scalled. The POD-struct is the size of the data members so there''s nothing init pointing to the functions apparently.Why would there need to be?Obviously *how* it actually works is an implementation detail. Allthat matters is that the required behaviour is exhibited. But I wouldthink something along the lines of this:You writestruct 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 ofstruct 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 looklike that - just illustrating what''s probably going on.How can the dot or -operatorsaccess the member functions? Some kind of special compiler-only overloadingof the member access operators that can distinguish between data member andmember function access?Given my struct foo above, the compiler knows which members arefunctions (func) and which are data members (i). When I doa_third_foo.i = 42;it knows which foo object I want to work with (a_third_foo), it knowswhere that object is in memory (wherever it put it) and it knowswhere, within that memory, the int i is that I want to set to 42.And when I doanother_foo.func();it knows I want to call a function (func), it knows where the code forthat function is (wherever it put it) and it knows which object topass the address of as the hidden "this" parameter (another_foo).Gavin Deane 这篇关于POD结构如何与成员函数相关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的..
09-06 19:47