我试图在C ++上创建虚拟运算符
class Data
{
virtual Matrix operator* (Matrix &_matrix);
virtual Scalar operator* (Scalar &_scalar);
};
class Matrix : public Data
{
private:
vector<vector<double>> data;
public:
// ...
Matrix operator* (Matrix &_matrix);
};
class Scalar : public Data
{
private:
double data;
public:
// ...
Scalar operator* (Scalar &_scalar);
};
问题是,当我创建Data * Array时,如下所示
Data* arr[10];
arr[0] = new Matrix(3,3);
arr[1] = new Matrix(3,3);
arr[0]->operator*(arr[1]);
我无法在这两个矩阵之间进行乘法运算,因为我不能将Data作为参数传递。 但问题是,我不能使函数的参数采用Data *类型,因为它将无法访问Matrix或Scalar对象的私有成员。
如何应对这种奇怪的情况?
答案 0 :(得分:6)
班级双重调度问题 - 见梅耶(忘了哪一个)。 您需要操作员在lhs和rhs上都是虚拟的,因此您需要两个虚拟调用:
class Matrix;
class Scalar;
class Data
{
public:
virtual Data* operator* (Data& data) = 0;
virtual Data* operator* (Matrix &matrix) = 0;
virtual Data* operator* (Scalar &scalar) = 0;
};
class Matrix : public Data
{
private:
std::vector<std::vector<double>> data;
// ...
public:
Matrix* operator* (Matrix &_matrix)
{
// implement
}
Matrix* operator* (Scalar& scalar)
{
// implement
}
Data* operator* (Data &data)
{
// Magic here - *this is now Matrix, not Data
return data * (*this);
}
};
class Scalar : public Data
{
private:
double data;
public:
// ...
Data* operator* (Data& data)
{
// Magic here - *this is now Scalar, not Data
return data * (*this);
}
Scalar* operator* (Scalar &scalar)
{
// implement
}
Matrix* operator* (Matrix &matrix)
{
// Note how we need to allow for parameter reveral during the double dispatch
Matrix& lhs = matrix;
Matrix& rhs = *this;
// Compute matrix product lhs * rhs
}
};
我已经忽略了返回类型和内存管理的问题。与运算符一样,您可能最好将*=
定义为基元。然后,这可以返回对*this
的引用。然后,*
运算符可以*=
来定义。再次,这是“有效的C ++”。
答案 1 :(得分:4)
对此的解决方案称为Double Dispatch,这在C ++中有点奇怪。以下代码显示了这个技巧的工作原理:
#include <iostream>
#include <stdexcept>
#include <vector>
class Matrix;
class Scalar;
class Data {
public:
virtual ~Data() {}
virtual Data const& operator* (Data const& other) const = 0;
virtual Data const& multiplyBy (Data const& other) const {
throw std::runtime_error("bad function call");
}
virtual Data const& multiplyBy (Matrix const& other) const = 0;
virtual Data const& multiplyBy (Scalar const& other) const = 0;
};
class Matrix : public Data {
private:
std::vector< std::vector<double> > data;
public:
virtual Data const& operator* (Data const& other) const {
return other.multiplyBy(*this);
}
virtual Data const& multiplyBy (Matrix const& other) const {
std::cout << "Matrix * Matrix" << std::endl;
return *this;
}
virtual Data const& multiplyBy (Scalar const& other) const {
std::cout << "Matrix * Scalar" << std::endl;
return *this;
}
};
class Scalar : public Data {
private:
double data;
public:
virtual Data const& operator* (Data const& other) const {
return other.multiplyBy(*this);
}
virtual Data const& multiplyBy (Matrix const& other) const {
std::cout << "Scalar * Matrix" << std::endl;
return *this;
}
virtual Data const& multiplyBy (Scalar const& other) const {
std::cout << "Scalar * Scalar" << std::endl;
return *this;
}
};
int
main() {
Matrix m;
Scalar s;
Data* ary[] = { &m, &s };
m * s;
s * m;
*(ary[0]) * *(ary[1]);
*(ary[1]) * *(ary[0]);
return 0;
}