在C++中使用本征3进行继承和铸造
Inheritence and casting with eigen3 in C++
我想如下扩展本征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更可取。
- 继承函数的重载解析
- 继承期间显示未知行为的子类
- 头文件-继承c++
- 为什么在保护模式下继承升级不起作用
- 通过继承类使用来自不同命名空间的运算符
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 混合组合和继承的C++问题
- 继承:构造函数,初始化C++11中基类的类C数组成员
- 从类继承时,继承的类是否会通过父类重新定义继承的变量
- 公共与私人继承
- 如何创建从同一类继承的不同对象的向量
- 如何从另一个文件继承私有成员变量和公共函数
- 在模板基类中为继承类中的可选重写生成虚拟方法
- 带有继承的C++工厂
- 我应该避免多重实现继承吗
- C++继承更改成员
- 从具有默认值的部分指定模板类继承时发生SWIG错误,具有不带默认值的正向声明
- 关于C++中具有多重继承"this"指针的说明
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- 如何在QT Creator上将QWidget声明为继承类的对象