xref: /aosp_15_r20/external/eigen/doc/examples/CustomizingEigen_Inheritance.cpp (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1*bf2c3715SXin Li #include <Eigen/Core>
2*bf2c3715SXin Li #include <iostream>
3*bf2c3715SXin Li 
4*bf2c3715SXin Li class MyVectorType : public Eigen::VectorXd
5*bf2c3715SXin Li {
6*bf2c3715SXin Li public:
MyVectorType(void)7*bf2c3715SXin Li     MyVectorType(void):Eigen::VectorXd() {}
8*bf2c3715SXin Li 
9*bf2c3715SXin Li     // This constructor allows you to construct MyVectorType from Eigen expressions
10*bf2c3715SXin Li     template<typename OtherDerived>
MyVectorType(const Eigen::MatrixBase<OtherDerived> & other)11*bf2c3715SXin Li     MyVectorType(const Eigen::MatrixBase<OtherDerived>& other)
12*bf2c3715SXin Li         : Eigen::VectorXd(other)
13*bf2c3715SXin Li     { }
14*bf2c3715SXin Li 
15*bf2c3715SXin Li     // This method allows you to assign Eigen expressions to MyVectorType
16*bf2c3715SXin Li     template<typename OtherDerived>
operator =(const Eigen::MatrixBase<OtherDerived> & other)17*bf2c3715SXin Li     MyVectorType& operator=(const Eigen::MatrixBase <OtherDerived>& other)
18*bf2c3715SXin Li     {
19*bf2c3715SXin Li         this->Eigen::VectorXd::operator=(other);
20*bf2c3715SXin Li         return *this;
21*bf2c3715SXin Li     }
22*bf2c3715SXin Li };
23*bf2c3715SXin Li 
main()24*bf2c3715SXin Li int main()
25*bf2c3715SXin Li {
26*bf2c3715SXin Li   MyVectorType v = MyVectorType::Ones(4);
27*bf2c3715SXin Li   v(2) += 10;
28*bf2c3715SXin Li   v = 2 * v;
29*bf2c3715SXin Li   std::cout << v.transpose() << std::endl;
30*bf2c3715SXin Li }
31