【发布时间】:2012-09-02 22:11:32
【问题描述】:
我在 C++ 中遇到了运算符重载的问题。
我定义了以下类:
template <class T>
class Array
{
public:
//! Default constructor
Array(int ArraySize = 10);
////! Defualt destructor
Array<T>::~Array();
//! Redefinition of the subscript operator
T& Array<T>::operator[] (int index);
//! Redefinition of the assignment operator
const Array<T>& Array<T>::operator=(const Array<T>&);
//! Redefinition of the unary operator -
Array<T>& operator-(Array<T>& a);
//! Array length
int size;
private:
//! Array pointer
T *ptr;
};
一元运算符 - 定义如下:
//! Redefinition of the unary operator -
template<class T>
Array<T>& operator-(Array<T>& a){
static Array<T> myNewArray(a.size);
for( int i = 0; i < a.size; i++){
myNewArray[i]=-a[i];
}
return myNewArray;
}
如何避免“myNewArray”在内存中的持久性?没有“静态”声明 当函数结束并且像 VectorA=-VectorB 这样的赋值失败时,myNewArray 会消失。
第二个问题是关于强制转换运算符的重载;我以这种方式重载了强制转换运算符:
//!CASTING
template <class B>
operator Array<B>(){
static Array<B> myNewArray(size);
.... a function makes the conversion and returns myNewArray populated...
return myNewArray;
}
但它不起作用!在使用静态声明执行函数后,对象 myNewArray 似乎也消失了。任何像 VectorA=(Array)VectorB 这样的赋值都会失败。
错误在哪里?请大家提出解决方案好吗? 提前谢谢!
【问题讨论】:
-
对于演员表操作员,再次,不需要
static。但是,如果没有编译和运行并显示问题的实际代码,就不可能说出出了什么问题。 -
尽可能避免使用强制转换运算符。很难预测何时触发隐式强制转换,这会导致很难找到错误。您应该考虑改为使用模板赋值运算符:
template <class B> Array& operator=(const Array<B>& rhs)。这将允许您将Array<B>显式转换为Array<T>。 -
您还可以编写一个模板化的显式构造函数,允许从
Array<B>中显式构造一个Array<T>。 -
您的
operator-不是一元的。它在*this和a上运行。VectorA = -VectorB怎么可能编译?我建议您尝试制作一个实际编译的小代码示例,您可以向我们展示——它总是很有帮助。 -
谢谢大家!我发现了问题:它是复制构造函数。函数的按值返回和命令 A=f(b) 不起作用,因为复制构造函数错误。重写后一切正常,无需静态和按引用返回。
标签: c++ templates casting operators overloading