简体   繁体   English

C ++中具有多态模板类的未知方法返回类型

[英]Unknown Method Return Type with Polymorphic Template Classes in C++

I have been struggling to figure out how to implement the following classes. 我一直在努力弄清楚如何实现以下类。 Essentially what I am trying to achieve is the following: - The main class is for a Matrix - The data should be stored external to the Matrix object - The Matrix_Data should be able to support both float and double types 本质上,我想实现的目标是:-主类用于Matrix-数据应存储在Matrix对象的外部-Matrix_Data应该能够支持浮点和双精度类型

I've been trying to work on figuring out how to implement with templates, but I cannot seem to figure it out. 我一直在努力弄清楚如何使用模板来实现,但是我似乎无法弄清楚。 This gives a general idea of my intent: 这大致说明了我的意图:

template <typename T>
class Matrix {
private:
   IMatrix_Data* m_data;
...
}

class IMatrix_Data { 
XXX Get_Data();  // <== The return type should be float or double, as appropriate
...
}

Can someone please give me some suggestions and some guidance? 有人可以给我一些建议和指导吗?

UPDATE: 更新:

I have updated the class following @ruslo's suggestion. 我已经按照@ruslo的建议更新了课程。 The problem that I'm now facing is that changing the Matrix and Matrix_Data into template classes causes a chain effect to a large number of classes. 我现在面临的问题是将Matrix和Matrix_Data更改为模板类会导致对大量类的连锁效应。 I admit that my experience with templates is very limited --- perhaps I'm going about this the wrong way, or perhaps this is the right way but it just looks wrong to me. 我承认我在模板方面的经验非常有限-也许我正在以错误的方式进行操作,或者这也许是正确的方法,但对我而言似乎是错误的。

Basically, it would seem that any class which then uses a Matrix, or the data stored in a matrix needs to be a template class. 基本上,似乎任何随后使用Matrix的类或存储在矩阵中的数据都必须是模板类。 I know that I can clean up the appearance of the code with typedef (or using statements), but I don't think that will change anything in terms of the hierarchy of classes, will it? 我知道我可以使用typedef(或使用语句)清理代码的外观,但是我认为这不会改变类的层次结构,对吗?

As an example of some classes which I have that use Matrix: 作为一些我使用矩阵的类的示例:

template <typename T>
class Vector : Matrix<T>

template <typename T>
class Input {  // <- this class is intended to be used as a base class for runtime polymorphism
    Vector<T>::DataType Get_Data();
    /* Rest of class */
};

class Parameterized_Input{ // <- this class is intended to be used as a base class for runtime polymorphism
};

template <typename T>    
class Input_Iterator {
    /* ...
           */
    std::stack<std::vector<Input<T>* >::iterator > parent_nodes; // Part of the iteration algo
}

I'm feeling rather confused here --- this is a little beyond anything I've done before and I'm hoping someone can help point me in the right direction here, both in terms of the implementation as well as any suggestions for improving the design. 我在这里感到很困惑---这超出了我之前做过的任何事情,我希望有人可以在实施以及改进方面的任何建议方面为我指明正确的方向该设计。

For example, as I noted in the code above, the class Input is intended to be an abstract base class, to allow runtime polymorphism. 例如,正如我在上面的代码中指出的那样,Input类旨在用作抽象基类,以允许运行时多态。 Derived classes will implement this interface (and possibly Parameterized_Input) to create different types of Inputs. 派生类将实现此接口(并可能实现Parameterized_Input)以创建不同类型的Inputs。 However, since the return type of the inputs is the same as that of the Matrix_Data -- ie unknown type T right now -- it seems that I will need to make every derived class into a template as well. 但是,由于输入的返回类型与Matrix_Data的返回类型相同(即,当前为未知类型T),看来我也需要将每个派生类也都放入模板中。

Unfortunately, at this time, I feel that I need the flexibility of using either a float for performance or double for precision, depending on the circumstances. 不幸的是,这时候,我觉得我需要根据情况灵活使用浮点数来提高性能,或者使用double来提高精度。 If I could rule one of these out, then it would certainly simplify everything. 如果我可以排除其中之一,那肯定会简化一切。

The alternative -- without templates, almost appears to be simpler in the big picture (based on my possible faulty understanding): 替代方案-没有模板,从总体上看似乎更简单(基于我可能的错误理解):

class Matrix{
    IMatrix_Data* m_data;
    /*  ...   */
}

class IMatrix_Data{
    /*  ...   */
    template <typename T>
    Get_Data(int _row,int _col) { return static_cast<T>( this->ncols * _col + _row ); }
}

namespace matrix_data {
    class MD_Double : public IMatrix_Data{
        /*  ...   */
    }

    class MD_Double : public IMatrix_Data{
        /*  ...   */
    }

I've tried to provide enough information above, but if there's anything missing, please let me know and I'll be happy to provide additional clarification and/or information. 我已尝试在上面提供足够的信息,但是如果有任何遗漏,请告知我,我们将很乐意提供其他说明和/或信息。

Thanks and regards, Shmuel 感谢和问候,Shmuel

Return value of GetData can be template parameter: GetData的返回值可以是模板参数:

template <class T>
class MatrixImpl {
 public:
  typedef T DataType;
  DataType GetData();
};

template <class T>
class Matrix {
 public:
  typedef MatrixImpl<T> Impl;
  typedef typename Impl::DataType DataType;

  DataType GetData() {
    return data_->GetData();
  }

 private:
  Impl* data_;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM