<bdo id='5rU6I'></bdo><ul id='5rU6I'></ul>

      <tfoot id='5rU6I'></tfoot>

    1. <i id='5rU6I'><tr id='5rU6I'><dt id='5rU6I'><q id='5rU6I'><span id='5rU6I'><b id='5rU6I'><form id='5rU6I'><ins id='5rU6I'></ins><ul id='5rU6I'></ul><sub id='5rU6I'></sub></form><legend id='5rU6I'></legend><bdo id='5rU6I'><pre id='5rU6I'><center id='5rU6I'></center></pre></bdo></b><th id='5rU6I'></th></span></q></dt></tr></i><div id='5rU6I'><tfoot id='5rU6I'></tfoot><dl id='5rU6I'><fieldset id='5rU6I'></fieldset></dl></div>
    2. <small id='5rU6I'></small><noframes id='5rU6I'>

        <legend id='5rU6I'><style id='5rU6I'><dir id='5rU6I'><q id='5rU6I'></q></dir></style></legend>

        对象的成员函数存储在哪里?

        时间:2023-09-17
        <i id='ZCFcq'><tr id='ZCFcq'><dt id='ZCFcq'><q id='ZCFcq'><span id='ZCFcq'><b id='ZCFcq'><form id='ZCFcq'><ins id='ZCFcq'></ins><ul id='ZCFcq'></ul><sub id='ZCFcq'></sub></form><legend id='ZCFcq'></legend><bdo id='ZCFcq'><pre id='ZCFcq'><center id='ZCFcq'></center></pre></bdo></b><th id='ZCFcq'></th></span></q></dt></tr></i><div id='ZCFcq'><tfoot id='ZCFcq'></tfoot><dl id='ZCFcq'><fieldset id='ZCFcq'></fieldset></dl></div>

      1. <tfoot id='ZCFcq'></tfoot>
          <tbody id='ZCFcq'></tbody>
      2. <small id='ZCFcq'></small><noframes id='ZCFcq'>

            1. <legend id='ZCFcq'><style id='ZCFcq'><dir id='ZCFcq'><q id='ZCFcq'></q></dir></style></legend>
              • <bdo id='ZCFcq'></bdo><ul id='ZCFcq'></ul>
                  本文介绍了对象的成员函数存储在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在试验 C++ 以了解类/结构及其各自的对象在内存中的布局方式,并且我了解类/结构的每个字段都是其各自对象的偏移量(因此我可以有一个成员变量指针).

                  I'm experimenting with C++ to understand how class/structures and their respective objects are laid out in memory and I understood that each field of a class/structure is an offset into their respective object (so I can have a member variable pointer).

                  我不明白为什么,即使我可以拥有成员函数指针,以下代码也不起作用:

                  I don't understand why, even if I can have member function pointers, the following code doesn't work:

                  struct mystruct
                  {
                      void function()
                      {
                          cout << "hello world";
                      }
                      int c;
                  };
                  
                  int main() 
                  { 
                      unsigned int offset_from_start_structure = (unsigned int)(&((mystruct*)0)->c);
                      unsigned int offset_from_start_structure2 = (unsigned int)(&((mystruct*)0)->function); // ERROR - error C2276: '&' : illegal operation on bound member function expression
                  
                  
                  
                      return 0;
                  }
                  

                  我的问题是:为什么行

                  unsigned int offset_from_start_structure = (unsigned int)(&((mystruct*)0)->c);
                  

                  编译并返回c"字段从结构和行开始处的偏移量

                  compile and returns me the offset of the "c" field from the start of the structure and the line

                  unsigned int offset_from_start_structure2 = (unsigned int)(&((mystruct*)0)->function);
                  

                  甚至不编译?

                  推荐答案

                  成员函数或指向它们的指针不存储在对象中.(virtual 函数通常通过存储在表中的指针调用,对象具有指向该表的单个指针)这将巨大浪费内存.它们通常存储在代码存储器部分中,并且为编译器所知.对象 (*this) 通常作为 invisible 参数传递,以便函数在调用时知道要操作的对象.

                  Member functions or pointers to them aren't stored in the object. (virtual functions are typically called through a pointer stored in a table to which an object has a single pointer to) This would be a huge waste of memory. They're typically stored in a code memory section, and are known to the compiler. The object (*this) is typically passed as an invisible parameter so the functions know on which object to operate when they are called.

                  所以,用外行的话来说,你会

                  So, in layman terms, you'd have

                   0x10001000    void A::foo
                   ..........    {code for A::foo}
                  

                   push a;
                   call A::foo (0x10001000)
                   pop a;
                  

                  其中 a 是您在其上调用 foo 的对象.

                  where a is the object you're calling foo on.

                  这篇关于对象的成员函数存储在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:由于我无法返回局部变量,从 C 或 C++ 函数返回字 下一篇:为什么删除不破坏任何东西?

                  相关文章

                  最新文章

                  1. <tfoot id='zLwzp'></tfoot>
                    • <bdo id='zLwzp'></bdo><ul id='zLwzp'></ul>

                    <legend id='zLwzp'><style id='zLwzp'><dir id='zLwzp'><q id='zLwzp'></q></dir></style></legend>

                    <small id='zLwzp'></small><noframes id='zLwzp'>

                    <i id='zLwzp'><tr id='zLwzp'><dt id='zLwzp'><q id='zLwzp'><span id='zLwzp'><b id='zLwzp'><form id='zLwzp'><ins id='zLwzp'></ins><ul id='zLwzp'></ul><sub id='zLwzp'></sub></form><legend id='zLwzp'></legend><bdo id='zLwzp'><pre id='zLwzp'><center id='zLwzp'></center></pre></bdo></b><th id='zLwzp'></th></span></q></dt></tr></i><div id='zLwzp'><tfoot id='zLwzp'></tfoot><dl id='zLwzp'><fieldset id='zLwzp'></fieldset></dl></div>