• <tfoot id='9snJT'></tfoot>

  • <small id='9snJT'></small><noframes id='9snJT'>

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

        C++ 模板特化,明确调用可以是指针或引用的类型的方法

        C++ template specialization, calling methods on types that could be pointers or references unambiguously(C++ 模板特化,明确调用可以是指针或引用的类型的方法)

            <tbody id='Y8F1G'></tbody>
        1. <small id='Y8F1G'></small><noframes id='Y8F1G'>

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

                  <legend id='Y8F1G'><style id='Y8F1G'><dir id='Y8F1G'><q id='Y8F1G'></q></dir></style></legend>
                  本文介绍了C++ 模板特化,明确调用可以是指针或引用的类型的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  总结

                  有没有一种方法可以在不知道是指针或引用的模板化类型上调用类方法,并且不会出现编译器/链接器错误?

                  Is there a way to call a class method on a templated type that could be a pointer or a reference without knowing which and not get compiler/linker errors?

                  详情

                  我有一个模板化的 QuadTree 实现,它可以采用以下任何非平凡的用户定义类型:

                  I have a templated QuadTree implementation that can take any of the following non-trivial user-defined types:

                  //Abstract Base Class
                  a2de::Shape
                  
                  //Derived Classes
                  a2de::Point
                  a2de::Line
                  a2de::Rectangle
                  a2de::Circle
                  a2de::Ellipse
                  a2de::Triangle
                  a2de::Arc
                  a2de::Spline
                  a2de::Sector
                  a2de::Polygon
                  

                  但它们可以是指针或引用,因为它们都是从 a2de::Shape 派生的.所以特化声明为:

                  But they could be a pointer OR a reference as they are all derived from a2de::Shape. So the specializations are declared as:

                  template class QuadTree<a2de::Shape&>;
                  //...similar for all derived types as references.
                  
                  template class QuadTree<a2de::Shape*>;
                  //...similar for all derived types as pointers
                  

                  我遇到的问题是当间接(或缺少间接)未知并且由于模板而生成两组代码时,能够调用类方法:

                  The problem I am having is the ability to call a class method when the indirection (or lack thereof) is unknown and due to the templates, both sets of code are generated:

                  template<typename T>
                  bool QuadTree<T>::Add(T& elem) {
                  
                      //When elem of type T is expecting a pointer here
                      //-> notation fails to compile where T is a reference i.e.:
                      //template class QuadTree<a2de::Shape&>
                      //with "pointer to reference is illegal"
                  
                      if(elem->Intersects(_bounds) == false) return false;
                  
                      //...
                  }
                  

                  如果我更改上面的行以使用 .(点)符号:

                  If I change the above line to use the . (dot) notation:

                  template<typename T>
                  bool QuadTree<T>::Add(T& elem) {
                  
                      //When elem of type T is expecting a reference here
                      //. (dot) notation fails to compile where T is a pointer i.e.:
                      //template class QuadTree<a2de::Shape*>
                      //with "pointer to reference is illegal"
                  
                      if(elem.Intersects(_bounds) == false) return false;
                  
                      //...
                  
                  }
                  

                  如果我删除基于引用的类型以支持基于指针的类型(包括在 Quadtree 类的声明和使用中),我会收到错误 left of .<function-name>必须有类/结构/联合.

                  If I remove the reference-based types in favor of the pointer-based types (including in the declaration and usage of the Quadtree class) I get the error left of .<function-name> must have class/struct/union.

                  如果我删除基于指针的类型以支持基于引用的类型(包括在 Quadtree 类的声明和使用中),我将再次收到上述 对指针的引用是非法的.

                  If I remove the pointer-based type in favor of the reference-based types (including in the declaration and usage of the Quadtree class) I get the aforementioned reference to pointer is illegal again.

                  编译器:VS2010-SP1

                  compiler: VS2010-SP1

                  推荐答案

                  可以使用小重载函数将引用变成指针:

                  Small overloaded functions can be used to turn reference into pointer:

                  template<typename T>
                  T * ptr(T & obj) { return &obj; } //turn reference into pointer!
                  
                  template<typename T>
                  T * ptr(T * obj) { return obj; } //obj is already pointer, return it!
                  

                  现在不要这样做:

                   if(elem->Intersects(_bounds) == false) return false;
                   if(elem.Intersects(_bounds) == false) return false;
                  

                  这样做:

                   if( ptr(elem)->Intersects(_bounds) == false) return false;
                  

                  如果 elem 是一个引用,第一个重载 ptr 将被选中,否则第二个将被选中.两者都返回 pointer,这意味着无论代码中的 elem 是什么,表达式 ptr(elem) 将始终是一个 指针 可以用来调用成员函数,如上所示.

                  If elem is a reference, the first overload ptr will be selected, else the second will be selected. Both returns pointer, which means irrespective of what elem is in your code, the expression ptr(elem) will always be a pointer which you can use to invoke the member functions, as shown above.

                  因为 ptr(elem) 是指针,这意味着检查 nullptr 是个好主意:

                  Since ptr(elem) is pointer, which means checking it for nullptr be good idea:

                   if( ptr(elem) && (ptr(elem)->Intersects(_bounds) == false)) return false;
                  

                  希望有所帮助.

                  这篇关于C++ 模板特化,明确调用可以是指针或引用的类型的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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++ 引用发生变化)
                • <tfoot id='xDJJ3'></tfoot>
                    <tbody id='xDJJ3'></tbody>

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

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

                    • <bdo id='xDJJ3'></bdo><ul id='xDJJ3'></ul>

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