c++子类-重用重载操作符

C++ Child classes - reusing overloaded operators?

本文关键字:重载 操作符 -重 子类 c++      更新时间:2023-10-16

我在c++中遇到了一个问题-我有两个类:class vecclass vecD : public vec

类vec几乎重载了任何操作符——其中一些操作符(如+=)出于明显的原因返回类本身的对象。现在我的问题是:是否有可能重用vec的重载函数为vecD,但返回一个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
}

就像这样:

class vecD : public vec<long> {
     /*whatever makes it unique here, incl constructors/destructors*/
     int data;
     vecD& operator+=(const vecD& b) {
         vec<long>::operator+=(b); //here's the magic line
         data += b.data;
         return *this;
     }
     vecD& operator+=(const vec<long>& b) { //overload for base type [optional]
         vec<long>::operator+=(b); //here's the magic line
         return *this;
     }
     /*[etc]*/
};