/* A simple array class template that performs dynamic *//* memory management and casting to (T*), which allows *//* to use it as a usual array. */template <typename T>
class Array
{
public:
//Constructor
Array(unsignedlong size)
{
try
{
data = new T[size];
m_size = size;
}
catch(...)
{
cout << "Could not allocate " << size << " bytes." << endl;
data = NULL; m_size = 0;
}
}
//Typecast operatoroperator T*() { assert(data!=NULL); return data; }
//Subscript operator
T& operator[] (unsignedlong Index);
//Destructor
~Array() { if(data!=NULL) delete[] data; }
private:
T * data;
unsignedlong m_size;
};
template<typename T>
T& Array<T>::operator[] (unsignedlong Index)
{
assert(Index<m_size);
assert(data!=NULL);
return data[Index];
}
然而,当我这样使用它时:
Array<char> filename(5);
filename[0] = SomeVar;
GCC输出以下警告:
warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second: [enabled by default]
note: candidate 1: T& Array<T>::operator[](longunsignedint) [with T = char]
note: candidate 2: operator[](char*, int) <built-in>