C++ 在继承 class 中访问运算符函数
C++ Accessing operator function in inherited class
我目前正在编写一个应用程序,我在其中使用 Eigen-Library 进行复杂的 3D 数学运算。因为我需要不同的点和矢量 classes,我的 point3d-class 看起来像这样:
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);
};
现在我想创建这个 class 的成员函数,它允许我解析如下所示的 OBJ 文件行:
v 2.8 0.52 10.18
这样的观点。这就是我打算如何设计我的解析函数
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, " "));
}
}
我的问题是 Eigen 不允许我访问存储在 Vector4d 中的数据,如 this->x、this-y、this->z 等。相反,您通常会以 Vector4d v 的形式访问它; v[0]、v[1]、v[2] 等。我认为这样做的方式是使用 a
double Eigen::Vector4d::operator[](unsigned int index){...}
函数。
我不知道如何在派生 class 中访问该数据。我必须做什么才能在该函数中访问此数据,以便我可以写入 x、y、z 值?
你可以做到
(*this)[0]
依此类推才能调用基数classoperator[]
.
x
、y
、z
不是Eigen::Vector4d
的成员变量,而是方法。您可以使用 this->x()
等(或 Vector4d::x()
)从派生的 class 访问它们。同样有效的是 (*this)[0]
或 (*this)(0)
。如果您需要在 class 中频繁访问 x()
、y()
、z()
,您可以写一次:
using Vector4d::x;
using Vector4d::y;
using Vector4d::z;
然后仅使用 x()
、y()
、z()
即可访问它们。但是,这可能会导致一些阴影冲突(如果您以相同的方式命名局部变量)。
我目前正在编写一个应用程序,我在其中使用 Eigen-Library 进行复杂的 3D 数学运算。因为我需要不同的点和矢量 classes,我的 point3d-class 看起来像这样:
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);
};
现在我想创建这个 class 的成员函数,它允许我解析如下所示的 OBJ 文件行:
v 2.8 0.52 10.18
这样的观点。这就是我打算如何设计我的解析函数
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, " "));
}
}
我的问题是 Eigen 不允许我访问存储在 Vector4d 中的数据,如 this->x、this-y、this->z 等。相反,您通常会以 Vector4d v 的形式访问它; v[0]、v[1]、v[2] 等。我认为这样做的方式是使用 a
double Eigen::Vector4d::operator[](unsigned int index){...}
函数。
我不知道如何在派生 class 中访问该数据。我必须做什么才能在该函数中访问此数据,以便我可以写入 x、y、z 值?
你可以做到
(*this)[0]
依此类推才能调用基数classoperator[]
.
x
、y
、z
不是Eigen::Vector4d
的成员变量,而是方法。您可以使用 this->x()
等(或 Vector4d::x()
)从派生的 class 访问它们。同样有效的是 (*this)[0]
或 (*this)(0)
。如果您需要在 class 中频繁访问 x()
、y()
、z()
,您可以写一次:
using Vector4d::x;
using Vector4d::y;
using Vector4d::z;
然后仅使用 x()
、y()
、z()
即可访问它们。但是,这可能会导致一些阴影冲突(如果您以相同的方式命名局部变量)。