C++ 特征自定义类和函数参数
我正在尝试使用Eigen替换代码中当前使用的矩阵库。我有几个像这样的类,它们向基矩阵类添加自定义方法。在本例中,我将父类替换为Eigen:C++ 特征自定义类和函数参数,c++,eigen,C++,Eigen,我正在尝试使用Eigen替换代码中当前使用的矩阵库。我有几个像这样的类,它们向基矩阵类添加自定义方法。在本例中,我将父类替换为Eigen: #include <iostream> #include <eigen3/Eigen/Dense> class MyVectorType: public Eigen::Matrix<double, 3, 1> { public: MyVectorType(void) : Eigen::Ma
#include <iostream>
#include <eigen3/Eigen/Dense>
class MyVectorType: public Eigen::Matrix<double, 3, 1> {
public:
MyVectorType(void) :
Eigen::Matrix<double, 3, 1>() {
}
typedef Eigen::Matrix<double, 3, 1> Base;
// This constructor allows you to construct MyVectorType from Eigen expressions
template<typename OtherDerived>
MyVectorType(const Eigen::MatrixBase<OtherDerived>& other) :
Eigen::Matrix<double, 3, 1>(other) {
}
// This method allows you to assign Eigen expressions to MyVectorType
template<typename OtherDerived>
MyVectorType & operator=(const Eigen::MatrixBase<OtherDerived>& other) {
this->Base::operator=(other);
return *this;
}
void customMethod() {
//bla bla....
}
};
#包括
#包括
类MyVectorType:公共特征::矩阵{
公众:
MyVectorType(无效):
特征::矩阵(){
}
typedef特征::矩阵基;
//此构造函数允许您从特征表达式构造MyVectorType
模板
MyVectorType(常数特征::矩阵基和其他):
特征::矩阵(其他){
}
//此方法允许您将特征表达式指定给MyVectorType
模板
MyVectorType和运算符=(常量特征::MatrixBase和其他){
此->基本::运算符=(其他);
归还*这个;
}
void customMethod(){
//布拉布拉。。。。
}
};
最大的问题是,在方法中管理自定义类并不容易。例如:
void foo(MyVectorType& a) {
....
a.customMethod();
}
void foo(Eigen::Ref<MyVectorType::Base> a) {
....
a.customMethod(); <---can't call customMethod here
}
Eigen::Matrix<double, -1, -1, 0, 15, 15> m(3,1);
foo(m); <---can't call it with m;
Eigen::Map<Matrix<double, 3, 1> > map(m.data(), 3, 1);
Eigen::Ref<Matrix<double, 3, 1> > ref(map);
foo(ref); <---it works but I can't call custom method
void foo(MyVectorType&a){
....
a、 自定义方法();
}
void foo(本征::参考a){
....
a、 customMethod();至少有三种方法:
摆脱MyVectorType
并通过机制使customMethod
成为MatrixBase
的成员
摆脱MyVectorType
并使customMethod
成为一个免费函数
通过让它继承Ref
及其构造函数,专门化Eigen::Ref
,添加customMethod
方法,并通过调用内部自由函数customMethod\u impl
来分解这两个方法
试着为你的问题添加一个最小的例子,这样有人可能会在不知道eigen3的情况下帮助你。我也不清楚:(添加了示例,我希望现在更清楚。我想我需要做一些类似的事情,即使使用map也可以直接使用我的自定义类,不是吗?如果您真的想使用map::customMethod
,那么可以,但您仍然可以这样做:Ref tmp=map(…)
哦,好的,谢谢。如果文档中有一个示例,那就太好了。祝贺图书馆,它太棒了。继续努力吧。