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

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

    1. 如何声明相互引用的类?

      How can I declare classes that refer to each other?(如何声明相互引用的类?)
    2. <i id='ZuZty'><tr id='ZuZty'><dt id='ZuZty'><q id='ZuZty'><span id='ZuZty'><b id='ZuZty'><form id='ZuZty'><ins id='ZuZty'></ins><ul id='ZuZty'></ul><sub id='ZuZty'></sub></form><legend id='ZuZty'></legend><bdo id='ZuZty'><pre id='ZuZty'><center id='ZuZty'></center></pre></bdo></b><th id='ZuZty'></th></span></q></dt></tr></i><div id='ZuZty'><tfoot id='ZuZty'></tfoot><dl id='ZuZty'><fieldset id='ZuZty'></fieldset></dl></div>
          <tbody id='ZuZty'></tbody>
        <tfoot id='ZuZty'></tfoot>
      • <legend id='ZuZty'><style id='ZuZty'><dir id='ZuZty'><q id='ZuZty'></q></dir></style></legend>

        • <small id='ZuZty'></small><noframes id='ZuZty'>

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

                本文介绍了如何声明相互引用的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                自从我使用 C++ 已经很长时间了,我在类相互引用时遇到了一些麻烦.

                It's been a long time since I've done C++ and I'm running into some trouble with classes referencing each other.

                现在我有类似的东西:

                a.h

                class a
                {
                  public:
                    a();
                    bool skeletonfunc(b temp);
                };
                

                b.h

                class b
                {
                  public:
                    b();
                    bool skeletonfunc(a temp);
                };
                

                由于每个人都需要对另一个人的引用,我发现我不能在顶部执行彼此的#include,否则我最终会陷入各种奇怪的循环.

                Since each one needs a reference to the other, I've found I can't do a #include of each other at the top or I end up in a weird loop of sorts with the includes.

                那么我怎样才能让 a 可以使用 b 反之亦然,而不会造成循环 #include 问题?

                So how can I make it so that a can use b and vice versa without making a cyclical #include problem?

                谢谢!

                推荐答案

                你必须使用前向声明:

                a.h

                class b;
                class a
                {
                  public:
                    a();
                    bool skeletonfunc(b temp);
                }
                

                然而,在许多情况下,这会迫使您在方法调用或成员变量中使用引用或指针,因为您不能在两个类头中都拥有完整的类型.如果必须知道类型的大小,则需要使用引用或指针.但是,如果只需要方法声明,则可以使用该类型.

                However, in many situations, this can force you to work with references or pointers in your method calls or member variables, since you can't have the full types in both class headers. If the size of the type must be known, you need to use a reference or pointer. You can, however, use the type if only a method declaration is required.

                这篇关于如何声明相互引用的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                What is a dangling reference?(什么是悬空引用?)
                C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)
                  • <legend id='Xn1UW'><style id='Xn1UW'><dir id='Xn1UW'><q id='Xn1UW'></q></dir></style></legend>

                          <bdo id='Xn1UW'></bdo><ul id='Xn1UW'></ul>
                          <tfoot id='Xn1UW'></tfoot>

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

                            <tbody id='Xn1UW'></tbody>

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