为派生类实施内部操作员
Implementing inner operator for derived class
假设我有一个常规矩阵类,为此我已经实现了执行常规矩阵乘法的操作员 *。
这样的操作员具有以下签名:
Matrix operator*(const Matrix & ) const;
我现在希望为代表3x3矩阵的继承类Matrix3实现另一个 *运算符。
它将具有以下签名:
Matrix3 operator*(const Matrix3 &) const;
我正在寻找实施此操作员的正确方法,以重新使用已经为基类编写的代码,并最大程度地降低成本(即复制)。
这应该可以正常工作:
// Either return base class
Matrix operator*(const Matrix3& other) const
{
return Matrix::operator*(other);
}
// Or construct from a Matrix
Matrix3 operator*(const Matrix3& other) const
{
return Matrix3(Matrix::operator*(other));
}
// Either construct the Matrix data in the Matrix3
Matrix3(const Matrix& other)
{
// Initialize Matrix specifics
// Initialize Matrix3 specifics
}
// Or pass the Matrix to it's base class so it can take care of the copy
Matrix3(const Matrix& other) : Matrix(other)
{
// Initialize Matrix3 specifics
}
相关文章:
- 将函数类成员映射到类本身内部
- Boost Spirit,获取迭代器内部语义动作
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 内联函数中具有内部链接的全局变量
- 在函数内部的声明中初始化数组,并在外部使用它
- 如何在不知道向量大小的情况下输入向量内部的向量?
- 卷曲bracers内部结构的声明
- 从函数角度看ID到文件路径的内部与外部映射
- spdlog标头仅与外部fmt一起使用.spdlog错误:'内部':不是'fmt'
- <<操作员在下面的行中工作
- 如何在pugixml中获取节点的内部XML
- 使用C链接在函数内部创建C++模板
- 指针没有更新它在void函数内部指向的值
- 方法内部但循环仍得到预期的不合格id错误C++
- C++:具有内部链接的正向声明常量
- C++ 与操作员不匹配<<
- 为什么逗号运算符在运算符[]中打电话给操作员[],而不是在Operator()内部调用
- 为派生类实施内部操作员
- 从操作员内部获取有关对象的RTTI信息
- 操作员重载内部调用转换