<legend id='qxTXC'><style id='qxTXC'><dir id='qxTXC'><q id='qxTXC'></q></dir></style></legend>
      <tfoot id='qxTXC'></tfoot>

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

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

        作为对象的类成员 - 指针与否?C++

        时间:2023-09-15

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

            <bdo id='AsSlF'></bdo><ul id='AsSlF'></ul>

            • <tfoot id='AsSlF'></tfoot>
                  <tbody id='AsSlF'></tbody>

                  <legend id='AsSlF'><style id='AsSlF'><dir id='AsSlF'><q id='AsSlF'></q></dir></style></legend>
                  本文介绍了作为对象的类成员 - 指针与否?C++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如果我创建一个类 MyClass 并且它有一些私有成员 MyOtherClass,那么将 MyOtherClass 设为指针是否更好?就它在内存中的存储位置而言,让它不是指针又意味着什么?创建类时会创建对象吗?

                  If I create a class MyClass and it has some private member say MyOtherClass, is it better to make MyOtherClass a pointer or not? What does it mean also to have it as not a pointer in terms of where it is stored in memory? Will the object be created when the class is created?

                  我注意到 QT 中的示例通常在类成员是类时将类成员声明为指针.

                  I noticed that the examples in QT usually declare class members as pointers when they are classes.

                  推荐答案

                  如果我创建了一个类 MyClass 并且它有一些私有成员 MyOtherClass,那么将 MyOtherClass 设为指针是否更好?

                  If I create a class MyClass and it has some private member say MyOtherClass, is it better to make MyOtherClass a pointer or not?

                  你通常应该在你的类中将它声明为一个值.它将是本地的,出错的机会更少,分配更少——最终可能出错的事情更少,并且编译器总是可以知道它在指定的偏移量处,所以......它有助于优化和二进制减少几个级别.在某些情况下,您知道必须处理指针(即多态、共享、需要重新分配),通常最好仅在必要时使用指针 - 特别是当它是私有/封装的时.

                  you should generally declare it as a value in your class. it will be local, there will be less chance for errors, fewer allocations -- ultimately fewer things that could go wrong, and the compiler can always know it is there at a specified offset so... it helps optimization and binary reduction at a few levels. there will be a few cases where you know you'll have to deal with pointer (i.e. polymorphic, shared, requires reallocation), it is typically best to use a pointer only when necessary - especially when it is private/encapsulated.

                  就它在内存中的存储位置而言,让它不是指针意味着什么?

                  What does it mean also to have it as not a pointer in terms of where it is stored in memory?

                  它的地址将接近(或等于)this -- gcc(例如)有一些高级选项来转储类数据(大小、vtables、偏移量)

                  its address will be close to (or equal to) this -- gcc (for example) has some advanced options to dump class data (sizes, vtables, offsets)

                  创建类时会创建对象吗?

                  Will the object be created when the class is created?

                  是的 - MyClass 的大小将增加 sizeof(MyOtherClass),或者更多,如果编译器重新对齐它(例如到它的自然对齐)

                  yes - the size of MyClass will grow by sizeof(MyOtherClass), or more if the compiler realigns it (e.g. to its natural alignment)

                  这篇关于作为对象的类成员 - 指针与否?C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:当函数中未指定返回值时,C++ progs 如何获得它们 下一篇:未使用的成员变量是否占用内存?

                  相关文章

                  最新文章

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

                    <small id='1UF2h'></small><noframes id='1UF2h'>

                    <tfoot id='1UF2h'></tfoot>
                      <legend id='1UF2h'><style id='1UF2h'><dir id='1UF2h'><q id='1UF2h'></q></dir></style></legend>