我正在尝试使用 Eigen 库在 std=c++98 上实现精简的 Cell 类(几乎像在 Matlab 中一样)。请帮忙,因为感觉当前实现的类在适当的内存分配方面是蹩脚的...... 我选择的方法很可能是错误的(矢量风格)。
我当前的实现如下所示。
class Cell : public Eigen::Matrix<Eigen::dcomplex, -1, -1> {
private:
Eigen::Matrix<Eigen::dcomplex, -1, -1>* begin;
Eigen::Matrix<Eigen::dcomplex, -1, -1>* end;
int count;
Cell() {};
public:
/**
* @brief Building list of NxM Eigen::MatrixXcd nodes
* @param count_in num of Eigen::MatrixXcd nodes
* @param n number of rows of each Eigen::MatrixXcd
* @param m number of cols of each Eigen::MatrixXcd
*/
Cell(size_t count_in, size_t n, size_t m) :
count(count_in)
,begin(reinterpret_cast<Eigen::Matrix<Eigen::dcomplex, -1, -1>*>(new char[count_in * sizeof(Eigen::Matrix<Eigen::dcomplex, -1, -1>) * n * m ]))
,end(begin + count_in) {
Eigen::Matrix<Eigen::dcomplex, -1, -1>* it = begin;
for (; it != end; ++it) {
new (it) Eigen::Matrix<Eigen::dcomplex, -1, -1>(n, m); //special "placement new" - calling constructor at the current adress
}
}
Eigen::Matrix<Eigen::dcomplex, -1, -1>& operator[](size_t index) {
return *(begin + index);
}
size_t size() {
return count;
}
~Cell() {
delete [] reinterpret_cast<char*>(begin);
}
};