c++子类-重用重载操作符
C++ Child classes - reusing overloaded operators?
我在c++中遇到了一个问题-我有两个类:class vec
和class 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]*/
};
相关文章:
- 重载操作符+:表达式必须是整型或无作用域枚举类型
- 重载操作符
- 如何重载操作符==外模板类使用友元函数
- 重载*操作符,使其在左右两边都工作
- 重载操作符<对于非随机迭代器
- 在c++中重载操作符的时间和原因
- 如何在c++中重载=操作符来通过引用进行复制
- 如何在c++中获取定义为友元的重载操作符的地址
- 使用重载操作符的文件操作表达式没有给出预期的结果
- 重载操作符()
- 重载操作符()并在类内使用
- 类中的重载操作符+
- 定时使用重载操作符
- c++带类的重载操作符
- 用列表容器重载[]操作符
- 重载操作符=
- 任何重载操作符()的静态检测
- 重载操作符以处理类对象
- 在使用另一个类的类中重载操作符==
- 派生类和基类中的重载操作符不同