This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Eigen::Matrix and virtual function

Tags: None
(comma "," separated)
wenluyang
Registered Member
Posts
5
Karma
0

Eigen::Matrix and virtual function

Wed Oct 21, 2015 7:13 am
Hi,
I am a freshman to this c++ software and use Eigen to bulid my FEM code.
Now,I have a vitrual function IniEle(),and want to initizate the element just like follow:
Code: Select all
class Element
{
public:
     virtual void IniElement(MatrixXd&,....)=0;
....
}

class QuadEle:public Element       //Quadrilateral element
{
public:
     void IniElement(Matrix<double,4,2>& coor,....) ;     //4*2Matrix contains four vertex Coordinates
......
}

class TriEle::public Element          //Triangular element
{
public:
     void IniElement(Matrix<double,3,2>& coor,...);       //3*2Matrix contains Three vertex Coordinates
...
}


Unfortunately,compiler throws error and I have to try another way.
Anyone can help with this problem?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
One solution is to use templated code instead of runtime polymorphism. If you want to keep the current architecture, then the virtual IniElement method must always have the exact same prototype. To this end, you can use the Ref<MatrixXd> type:

void IniElement(Ref<MatrixXd> coord, ...);

which can reference any matrix or block of matrix similar to a MatrixXd.

In case one implementation of IniElement can benefit from compile-time sizes, then you can still cast it to a Ref of appropriate size within the method:

Ref<Matrix<double,3,2> > actual_coord(coord);


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], q.ignora, watchstar