1. <tfoot id='elARg'></tfoot>

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

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

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

        operator= 和 C++ 中未继承的函数?

        时间:2023-08-02
            <tbody id='yoLs5'></tbody>

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

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

                  本文介绍了operator= 和 C++ 中未继承的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  在我刚刚进行的测试之前,我认为只有构造函数在 C++ 中没有被继承.但显然,赋值 operator= 不是太......

                  Until a test I've just made, I believed that only Constructors were not inherited in C++. But apparently, the assignment operator= is not too...

                  1. 这是什么原因?
                  2. 是否有任何解决方法可以继承赋值运算符?
                  3. operator+=, operator-=, ... 也是这种情况吗?
                  4. 是否继承了所有其他函数(除了构造函数/operator=)?
                  1. What is the reason of that ?
                  2. Is there any workaround to inherit the assignment operator ?
                  3. Is it also the case for operator+=, operator-=, ... ?
                  4. Are all other functions (apart from constructors/operator=) inherited ?

                  事实上,我在做一些CRTP时遇到了这个问题:

                  In fact, I encountered this problem as I was doing some CRTP :

                  template<class Crtp> class Base
                  {
                      inline Crtp& operator=(const Base<Crtp>& rhs) {/*SOMETHING*/; return static_cast<Crtp&>(*this);}
                  };
                  
                  class Derived1 : public Base<Derived1>
                  {
                  };
                  
                  class Derived2 : public Base<Derived2>
                  {
                  };
                  

                  有什么解决方案可以让它工作吗?

                  Is there any solution to get that working ?

                  好的,我已经隔离了问题.为什么以下不起作用?如何解决问题?

                  EDIT : OK, I have isolated the problem. Why the following isn't working ? How to solve the problem ?

                  #include <iostream>
                  #include <type_traits>
                  
                  // Base class
                  template<template<typename, unsigned int> class CRTP, typename T, unsigned int N> class Base
                  {
                      // Cast to base
                      public:
                          inline Base<CRTP, T, N>& operator()()
                          {
                              return *this;
                          }
                  
                      // Operator =
                      public:
                          template<typename T0, class = typename std::enable_if<std::is_convertible<T0, T>::value>::type>
                          inline CRTP<T, N>& operator=(const T0& rhs)
                          {
                              for (unsigned int i = 0; i < N; ++i) {
                                  _data[i] = rhs;
                              }
                              return static_cast<CRTP<T, N>&>(*this);
                          }
                  
                      // Data members
                      protected:
                          T _data[N];
                  };
                  
                  // Derived class
                  template<typename T, unsigned int N> class Derived : public Base<Derived, T, N>
                  {
                  };
                  
                  // Main
                  int main()
                  {
                      Derived<double, 3> x;
                      x() = 3; // <- This is OK
                      x = 3;   // <- error: no match for 'operator=' in ' x=3 '
                      return 0;
                  }
                  

                  推荐答案

                  赋值运算符在技术上是继承的;然而,它总是被派生类的显式或隐式定义的赋值运算符隐藏(参见下面的注释).

                  The assignment operator is technically inherited; however, it is always hidden by an explicitly or implicitly defined assignment operator for the derived class (see comments below).

                  (13.5.3 赋值) 赋值运算符应由只有一个参数的非静态成员函数.因为是副本赋值运算符 operator= 是为类隐式声明的,如果未由用户声明,基类赋值运算符始终是被派生类的复制赋值运算符隐藏.

                  (13.5.3 Assignment) An assignment operator shall be implemented by a non-static member function with exactly one parameter. Because a copy assignment operator operator= is implicitly declared for a a class if not declared by the user, a base class assignment operator is always hidden by the copy assignment operator of the derived class.

                  您可以实现一个虚拟赋值运算符,它只是将调用转发到基类 operator=,如下所示:

                  You can implement a dummy assignment operator which simply forwards the call to the base class operator=, like this:

                  // Derived class
                  template<typename T, unsigned int N> class Derived : public Base<Derived, T, N>
                  {
                  public:
                      template<typename T0, class = typename std::enable_if<std::is_convertible<T0, T>::value>::type>
                      inline Derived& operator=(const T0& rhs)
                      {
                          return Base<Derived, T, N>::operator=(rhs);
                      }
                  };
                  

                  这篇关于operator= 和 C++ 中未继承的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:更改派生类中的函数访问模式 下一篇:访问另一个子类中基类的受保护成员

                  相关文章

                  最新文章

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

                2. <small id='NGbAx'></small><noframes id='NGbAx'>

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

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