首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类继承和运算符重载

类继承和运算符重载
EN

Stack Overflow用户
提问于 2018-07-31 06:33:06
回答 1查看 243关注 0票数 0

我正在通过一个关于SFML的小项目学习C++,我想扩展sf::Vector2<T>类,它表示一个2D数学向量。这个类的声明是available here。特别是,我想在类中添加一些方法,用于范数计算、旋转等。

这就是我到目前为止所知道的:

代码语言:javascript
复制
#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;
    }
};

在后面的代码中,我声明了这样一个结构:

代码语言:javascript
复制
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
        );
    }
}

Derivativeoperator+重载的返回部分无法编译:

代码语言:javascript
复制
/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.dPossf::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。我哪里错了?我该怎么让它工作呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-05 23:08:02

sf::Vector<T>不是设计用来作为派生类使用的,因为这样一种更好的方法是编写免费函数来扩展sf::Vector<T>的功能,这对于操作员来说是无缝工作的。不幸的是,对于普通的函数调用,C++还不像C#那样支持扩展。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51603157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档