是否可以定义指向Eigen::Map对象的指针?原始代码非常复杂,但这是我想要实现的内容(伪代码)
void testfunction1(... XPtr){
// XPtr is a pointer
// create a vector, map it to a Map object and make XPtr point to the latter
VectorXd Xnew(9);
Xnew << 10, 20, 30, 40, 50, 60, 70, 80, 90;
Map<VectorXd> XnewMap(Xnew.data(), 9);
// make XPtr point to XnewMap so that Xnew data can be
// accessed outside testfunction1()
// ... how? I suspect this to involve some dynamic memory allocation
};
void testfunction2(bool yes){
// main function
VectorXd XR(9);
XR << 1, 2, 3, 4, 5, 6, 7, 8, 9;
const Map<VectorXd> X(XR.data(), 9); // yes the mapped version is needed
// create a pointer to X, say XPtr
// ... how?
if(yes){ // make XPtr point to XnewMap which is defined in testfunction1()
testfunction1(XPtr);
};
//... some computations
// make XPtr point again to X
// ... how?
};
typedef const MapVectorXd ConstMapVectorXd;
,然后使用ConstMapVectorXd*
,但是您必须处理新Map引用的数据和Map对象本身的复杂动态内存分配,这常常容易出现错误和内存泄漏。如果您使用placement new修改Map对象本身,则“仅”需要处理所引用数据的分配/释放,但这并不理想。 - ggael