类继承和运算符重载
Class inheritance and operator overloading
我正在通过SFML上的一个小项目学习C++,我想扩展sf::Vector2<T>
类,它代表一个2D数学向量。此类的声明可在此处获得。特别是,我想在类中添加一些方法,用于范数计算、旋转等。
这是我到目前为止所拥有的:
#include <cmath>
#include <SFML/System/Vector2.hpp>
#include <SFML/Graphics/Transform.hpp>
#include <ostream>
namespace dw::math {
template <typename T>
class Vector2 : public sf::Vector2<T> {
public:
// Don't really know what does that exactly means, explainations are welcome !
using sf::Vector2<T>::Vector2;
template <typename U>
explicit Vector2(const U &vector) : sf::Vector2<T>(vector) {}
// This ctor is for implicit conversion from base, don't know if it's really useful ?
Vector2(sf::Vector2<T> &vector) : sf::Vector2<T>(vector) {}
// Some methods here ...
friend std::ostream & operator<<(std::ostream &os, const math::Vector2<T>& right) {
os << '{' << right.x << ", " <<right.y << '}';
return os;
}
};
在代码的后面,我声明了这样一个结构:
struct Derivative {
dw::math::Vector2f dPos;
dw::math::Vector2f dVel;
Derivative() = default;
Derivative(const dw::math::Vector2f &dPos, const dw::math::Vector2f &dVel) : dPos(dPos), dVel(dVel) {}
Derivative operator+(const Derivative &rhs) const {
return Derivative(
dPos + rhs.dPos,
dVel + rhs.dVel
);
}
}
Derivative
的operator+
重载的返回部分不编译:
/home/palra/Documents/Projets/dw/src/physics/World.cpp: In member function ‘Derivative Derivative::operator+(const Derivative&) const’:
/home/palra/Documents/Projets/dw/src/physics/World.cpp:24:18: error: invalid user-defined conversion from ‘sf::Vector2<float>’ to ‘const Vector2f& {aka const dw::math::Vector2<float>&}’ [-fpermissive]
dPos + rhs.dPos,
~~~~~^~~~~~~~~~
In file included from /home/palra/Documents/Projets/dw/src/physics/Body.h:8:0,
from /home/palra/Documents/Projets/dw/src/physics/World.h:10,
from /home/palra/Documents/Projets/dw/src/physics/World.cpp:5:
/home/palra/Documents/Projets/dw/src/physics/../math/Vector2.h:30:5: note: candidate is: dw::math::Vector2<T, <template-parameter-1-2> >::Vector2(sf::Vector2<T>&) [with T = float; <template-parameter-1-2> = void] <near match>
Vector2(sf::Vector2<T> &vector) : sf::Vector2<T>(vector) {}
我不明白为什么这不起作用。 表达式dPos + rhs.dPos
与sf::Vector2<float> operator +(const sf::Vector2<float>& left, const sf::Vector2<float>& right)
的调用兼容,因为dw::math::Vector2<float>
是通过继承sf::Vector2<float>
。然后这个表达式应该产生一个sf::Vector2<float>
,由于我猜是非显式构造函数,它可以分配给dw::math::Vector2f
。我错在哪里?我应该如何让它工作?
sf::Vector<T>
不是为了用作派生类而设计的,因为更好的方法是编写扩展sf::Vector<T>
功能的自由函数,这对运算符来说是无缝的。不幸的是,对于普通函数调用,C++(还)不像 C# 那样支持扩展。
相关文章:
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 重载运算符new[]的行为取决于析构函数
- 为什么将值返回函数传递给重载=运算符对运算符函数有效,而对其他运算符无效
- 在 myVector 类中重载运算符 + 时出错
- 为什么常量词在重载运算符中不与 ostream 对象一起使用<<?
- 如何在 cpp 中重载运算符 +=?
- C++ 如何重载 [] 运算符并进行函数调用
- 重载运算符的范围是什么?它是否会影响作为类成员的集合的插入函数?
- 为什么我可以在不重载 "=" 运算符的情况下将一个对象分配给另一个对象?
- 重载运算符有地址吗?
- 如何迭代重载运算符 [] 的类?
- 重载运算符与添加问题
- 模板基类中的重载运算符
- 如何调用用于重载运算符"<<"的 friend 函数?
- 在 C++17 中的命名空间和子命名空间中重载运算符是不明确的
- 重载运算符<<采用谷歌 C++ 风格
- C++ 如何正确重载 + 运算符
- cout (<<) 重载运算符不打印减去的矩阵
- 如何在 c++ 中重载运算符 + 以便能够 whrite c_str = "smth" + c_str;
- 重载运算符*以获取对另一个类的实例的引用