gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:33:00 27 4
gpt4 key购买 nike

我在使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com