【问题标题】:Call the cast operator of template base class within the derived class在派生类中调用模板基类的强制转换运算符
【发布时间】:2012-06-21 23:57:52
【问题描述】:

我有一个模板类,叫做Cell,这里是定义:

template <class T>
class OneCell
{
.....
}

我有一个从Cell 到 T 的转换运算符,这里

virtual operator const T() const
{
   .....
}

现在我有派生类,称为DCell,在这里

template <class T>
class DCell : public Cell<T>
{
.....
}

我需要重写 Cell 的演员表操作符(插入一点 if),但之后我需要调用 Cell 的演员表操作员。在其他方法中,它应该类似于

virtual operator const T() const
{
    if (...)
    {
        return Cell<T>::operator const T;
    }
    else throw ...
}

但我遇到了编译器错误

错误:“const int (Cell::)()const”类型的参数与“const int”不匹配

我能做什么?

谢谢你,对不起我的英语不好。

【问题讨论】:

  • 我把整个代码都放好了,这样会更好

标签: c++ templates inheritance casting virtual


【解决方案1】:

不要让操作员虚拟化。而是委托给 protected virtual 辅助函数。

template <class T>
class Cell
{
    public:
        operator const T() const { return cvt_T(); }
    protected:
        virtual const T cvt_T() const;
};

template <class T>
class DCell : public Cell<T>
{
    const T cvt_T() const
    {
        if (...)
        {
            return Cell<T>::cvt_T();
        }
        else throw ...
    }
};

可以向 GotW here is the section on virtual architecture 学习这些和其他良好做法。

【讨论】:

  • @KerrekSB 我认为cvt_T 应该是。
  • @Kerrek, Luchian:感谢您指出这一点。 Luchian 是正确的。
【解决方案2】:

您实际上并没有调用操作员:

return Cell<T>::operator const T();

完整代码:

template <class T>
class OneCell
{
public:
    virtual operator const T() const
{
        return T();
    }
};

template <class T>
class DCell : public OneCell<T>
{
public:
    virtual operator const T() const
    {
        cout << "operator called";
        return OneCell<T>::operator const T();
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    DCell<int> x;
    int y = (int)x;
}

【讨论】:

    【解决方案3】:

    你缺少括号,所以编译器认为你试图返回成员函数,而不是调用它。

            return Cell<T>::operator const T();
    

    【讨论】:

      【解决方案4】:

      将此代码与CellDCell 的实现一起考虑:

      #include <iostream>
      #include <exception>
      
      template<class T>
      class Cell
      {
      protected:
          T cnt;
      public:
          Cell(const T& cnt = T()) : cnt(cnt){}
          virtual operator const T() const { return cnt; }
      };
      
      bool test_bool = true;
      
      template<class T>
      class DCell : public Cell<T>
      {
      public:
          DCell(const T& cnt = T()) : Cell<T>(cnt){}
          virtual operator const T() const
          {
              if(test_bool)
              {
                  return Cell<T>::operator const T(); // Here you had Cell<T>::operator const T;
              } else {
                  throw std::exception();
              }
          }
      };
      
      int main()
      {
          DCell<int> cell(5);
          std::cout << static_cast<int>(cell) << "\n"; // prints 5 (and a new line)
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-30
        • 1970-01-01
        • 2017-01-13
        • 1970-01-01
        • 2015-03-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多