Array(Array & other); 46 error C2664: 'char *strcpy(char *,const char *)' : cannot convert argument 1 from 'int *' to 'char *' strcpy, , . , - cannot convert argument 1 from 'std::string *' to 'char *'
, strcpy char . , T , strcpy , . ( ) :
template <class T>
Array<T>::Array(const Array &other) : size(other.size), arr(new T[size])
{
for (int i = 0; i < size; ++i)
arr[i] = other.arr[i];
}
2) strcpy 71 operator=. cannot convert argument 1 from 'std::string *' to 'char *'
, , : C-style str* raw char * , . , std::string ++ - . , std::string , .
:
template <class T>
Array<T>& Array<T>::operator = (Array rhs)
{
std::swap(size, rhs.size);
std::swap(arr, rhs.arr);
return *this;
}
3) T & operator[](int i); //allow read and write 85 , i . , . , Array<T>::T & operator[] (int n) const; , , n .
operator [] Array<T> i. :
Array<int> arr(42);
std::cout << arr[0];
, - . const, non-const. , const, const Array<T>:
template <class T>
T& Array<T>::operator[] (int i)
{
assert(0 <= i && i < size);
return arr[i];
}
template <class T>
const T& Array<T>::operator[] (int i) const
{
assert(0 <= i && i < size);
return arr[i];
}
4) void print(int n = 5);. , , , . for, .
5) , void print(ostream & out, int n);. ?
, , "". "" ++, .
, , print ( cout btw). print . :
void print(ostream &out, int n)
{
for (size_t i = 0; i < n; ++i)
out << pets[i] << " ";
}
void print(int n)
{
print(cout, n);
}
. print, , , , (?) .
: print, , , , . :
template <typename T>
void print(const Array<T> &arr, int n)
{
print(arr, n, cout);
}