C++ 子类 - 重用重载运算符?

标签 c++ class inheritance operator-overloading

我在使用 C++ 时遇到了一些问题 - 我有两个类: class vecclass vecD : public vec

vec 类几乎重载了任何运算符——其中一些运算符(如 +=)出于显而易见的原因返回类本身的对象。现在我的问题是:是否可以为 vecD 重用 vec 的重载函数,而是返回一个 vecD 对象?我的意思是肯定可以定义所有虚拟函数并重新定义它们......但是,你知道必须有一些更流畅的方法吗?

编辑:抱歉,确定一些代码:是否可以像那样使用继承?从模板化类开始?我可以重用父类中的运算符吗?

template<typename TYPE>
class vec{
public:

    TYPE *val;
    int dimension;
public:

    vec();
    ~vec();


    TYPE operator[](int right);
    virtual vec<TYPE>& operator=( TYPE right );
    virtual vec<TYPE>& operator=( vec<TYPE> right );
    virtual vec<TYPE> operator+( TYPE right );
    virtual vec<TYPE> operator-( TYPE right );
    [etc]
};

class vecD : public vec<long>{
public:
    long *X;
    long *Y;
    long *Z;
    long *Xmax;
    long *Ymax;
    long *Zmax;

public:
    vecD();
    ~vecD();

    long getAddress();
    long setAddress( long _X, long _Y, long _Z );


    TYPE operator[](int right);
    virtual vecD<long>& operator=( long right );
    virtual vecD<long>& operator=( vecD<long> right );
    virtual vecD<long>& operator=( vec<long> right );

    virtual vecD<long> operator+=( vecD<long> right );
    [etc]
};

最佳答案

从评论来看,这可能也让您感兴趣:

//this is used as a common base, so that you don't have to write the gory 
//details twice.  Only once.
template<typename TYPE>
class vec_base{
protected:
    TYPE *val;
    int dimension;
    vec(); /*protected so only vec can use vec_base, nobody else*/
    ~vec();
    /* I don't see any reason for these to be virtual */
    TYPE operator[](int right);
    vec<TYPE>& operator=( TYPE right );
    vec<TYPE>& operator=( vec<TYPE> right );
    vec<TYPE> operator+( TYPE right );
    vec<TYPE> operator-( TYPE right );
    [etc]
};

//most types just use this.
//it's just a simple wrapper around vec_base<TYPE>
template<typename TYPE>
class vec : public vec_base<TYPE> {
    //no data
    vec(const vec_base<TYPE>& b) :vec_base<TYPE>(b) {}
    vec(vec_base<TYPE>&& b) :vec_base<TYPE>(b) {}
public: 
    vec() {}
    vec(const vec& b) :vec_base<TYPE>(b) {}
    vec(vec&& b) :vec_base<TYPE>(b) {}
    ~vec() {}
    vec<TYPE>& operator=(const vec<TYPE>& b) 
    {vec_base<TYPE>::operator=(b); return *this;}
    vec<TYPE>& operator+=(const vec<TYPE>& b) 
    {vec_base<TYPE>::operator+=(b); return *this;}
    vec<TYPE> operator+(const vec<TYPE>& b) 
    {return vec<TYPE>(vec_base<TYPE>::operator+(b));}
    vec<TYPE>& operator-=(const vec<TYPE>& b) 
    {vec_base<TYPE>::operator-=(b); return *this;}
    vec<TYPE> operator-(const vec<TYPE>& b) 
    {return vec<TYPE>(vec_base<TYPE>::operator-(b));}
    /*other overloads that return vec_base<TYPE>*/
    /*all they do is call the base, super easy.*/
};

//when someone wants <long>, they get this instead
//which is vec_base, but so much more
//(not that I know what that is)
class vec : public vec_base<long>{
public:
    long *X;
    long *Y;
    long *Z;
    long *Xmax;
    long *Ymax;
    long *Zmax;
public:
    vecD() : vec_base<long>(), X(nullptr), Y(nullptr), Z(nullptr), /*etc*/ {}
    ~vecD() {delete[] X; delete[] Y; delete[] Z;}

    /* vec<long> can have unique functions */
    long getAddress();
    long setAddress( long _X, long _Y, long _Z );

    /* some are simple wrappers, some you have to play with X/Y/Z*/
    TYPE operator[](int right)
    {return vec_base<long>::operator[](right);}
    vec<long>& operator=( long right ) 
    {
         vec_base<long>::operator=(right);
         return *this;
    }
    vec<long>& operator=( vec<long> right )
    {
         vec_base<long>::operator=(right);
         X = right.X;
         Y = right.Y;
         Z = right.Z;
         return *this;
    }
    vec<long> operator+=( vecD<long> right );
    [etc]
    /*other overloads all must be overridden*/
    /*if they touch X,Y,Z,etc.*/
};

int main() {
    vec<int> a; //this is what you already had as vec<TYPE>
    vec<long> b; //this is what you already had as vec<TYPE>
    b.getAddress();  //this is allowed on b, but not a
}

关于C++ 子类 - 重用重载运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7274493/

相关文章:

c++ - 在 C++ ctor-initializer 中可选地包含成员的干净方法

java - 将 Class 对象传递给方法

javascript,多态 new Myclass()?

python - 如何使用 __str__ 更改对象属性的显示方式?

c# - HiddenField 不是 WebControl?

java - 将通用子类型类信息传递给 Java 中的父类(super class)

c++ - 错误 C1010 : unexpected end of file while looking for precompiled header. 您是否忘记将 '#include "stdafx.h"' 添加到您的源代码中?

c++ - Qt SDK : qhash. h error: C2872: 'uint' : ambiguous symbol

c++ - 加载共享库时出错

c# - 从实现类中的接口(interface)继承注释?