Search code examples
c++inheritanceeigen

C++ Accessing operator function in inherited class


I am currently writing an application where I use the Eigen-Library for complex 3D-Math. Because I needed distinct point and vector classes, my point3d-class looks like this:

class point3d : public Eigen::Vector4d
{
public:
    point3d(){}
    point3d(double x, double y, double z) : Eigen::Vector4d(x, y, z, 1) {}
    void fromString(std::string input);
};

Now I want to create a member function of this class that allows me to parse lines of OBJ-files which look like this:

v 2.8 0.52 10.18

as such point. This is how I intend to design my parsing function

void point3d::fromString(std::string input)
{
    char* buf = strdup(input.c_str());
    if (strtok(buf, " ") == "v")
    {

        ;
        strtok(buf, " ");
        this-x = std::stod(strtok(buf, " "));
        this->y = std::stod(strtok(buf, " "));
        this->z = std::stod(strtok(buf, " "));  

    }
}

My problem is that Eigen does not allow me to access the data stored in Vector4d as this->x, this-y, this->z and so on. Instead, you'd usually access it as Vector4d v; v[0], v[1], v[2] etc. I think the way it does that is by using a

double Eigen::Vector4d::operator[](unsigned int index){...}

function.

I don't know how exactly one would access that data in a derived class. What do I have to do to access this data within that function so that I can write to the x, y, z values?


Solution

  • You can do

    (*this)[0]
    

    and so on in order to call the base class operator[].