在C++中使用本征3进行继承和铸造

Inheritence and casting with eigen3 in C++

本文关键字:继承 C++      更新时间:2023-10-16

我想如下扩展本征3型:

typedef Eigen::Matrix<unsigned char, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> CMatrixImgParent;
class CMatrixImg : public  CMatrixImgParent
{
  public:
    CMatrixImg() : CMatrixImgParent() {}
    int Dummy(const char *filename) const {};
};

然后用本征3做一些算术运算。

CMatrixImg img1, img2, imgSum;
imgSum = img1 + img2;

但这不起作用,因为我用g++得到错误:

g++ -o bug.o -c -O2 -I/usr/include/eigen3 bug.cc
bug.cc: In function 'int main(int, char**)':
bug.cc:17:10: error: no match for 'operator=' (operand types are 'CMatrixImg' and 'const Eigen::CwiseBinaryOp<Eigen::internal::scalar_sum_op<unsigned char>, const Eigen::Matrix<unsigned char, -1, -1, 1>, const Eigen::Matrix<unsigned char, -1, -1, 1> >')
   imgSum = img1 + img2;
          ^
bug.cc:17:10: note: candidate is:
bug.cc:5:7: note: CMatrixImg& CMatrixImg::operator=(const CMatrixImg&)
 class CMatrixImg : public  CMatrixImgParent
       ^
bug.cc:5:7: note:   no known conversion for argument 1 from 'const Eigen::CwiseBinaryOp<Eigen::internal::scalar_sum_op<unsigned char>, const Eigen::Matrix<unsigned char, -1, -1, 1>, const Eigen::Matrix<unsigned char, -1, -1, 1> >' to 'const CMatrixImg&'
scons: *** [bug.o] Error 1
scons: building terminated because of errors.
Compilation exited abnormally with code 2 at Tue Jul 16 18:31:18

当然,我可以通过一些明确的选角来解决这个问题,比如:

(*(CMatrixImgParent*)&imgSum) = img1 + img2;

但这是非常丑陋的。

有什么简单的代码可以放在类定义中来绕过这种类型的强制转换的需要吗?

Eigen的文档建议,从Eigen::Matrix(在您的示例中本质上是CMatrixImgParent)继承应该只是最后的手段,并且最好使用允许您直接向Eigen::Matrix添加成员的宏驱动方法:

在从Matrix继承之前,我的意思是,确实要确保使用EIGEN_Matrix_PLUGIN不是您真正想要的(请参阅上一节)。如果你只需要在矩阵中添加几个成员,这就是你的选择。

宏观方法描述为:

在本节中,我们将了解如何将自定义方法添加到MatrixBase中。由于所有表达式和矩阵类型都继承了MatrixBase,因此在MatrixBase中添加一个方法可使所有表达式立即使用它!例如,一个典型的用例是使Eigen与另一个API兼容。

您当然知道,在C++中,不可能将方法添加到现有的类中。那这怎么可能呢?这里的技巧是在MatrixBase的声明中包含一个由预处理器令牌EIGEN_MatrixBase_PLUGIN 定义的文件

他们举的例子是

class MatrixBase {
// methods ...
#ifdef EIGEN_MATRIXBASE_PLUGIN
#include EIGEN_MATRIXBASE_PLUGIN
#endif
};

因此,您似乎可以按照这种方法来定义int Dummy(const char* filename) const方法。如果您真的想从Eigen::Matrix继承,那么您似乎需要自己编写一个赋值运算符,正如注释中提到的n.m。

将赋值运算符从CMatrixImgParent添加到CMatrixImg类解决了强制转换问题:

class CMatrixImg : public  CMatrixImgParent
{
  public:
    // Constructor
    CMatrixImg() : CMatrixImgParent() {}
    // This is needed for assignment of arithmetic results.
    CMatrixImg& operator=(const CMatrixImgParent& Other) {
      *((CMatrixImgParent*)this) = Other;
      return *this;
    }
    int Dummy(const char *filename) const {};
};

但也可以参见RyanMcK的答案,这可能对本征3更可取。