0

I'm just trying to get a handle on the when and why of overloaded operators.

I the following snippet:
the line: T& operator[] ... is there for use in the copy constructor?

the line : const T& operator [] ... is there for the constructor definition?

In english, the code is instructing the compiler to return an array pointer of pType[] when encountering an array refernce from T.

this is what enables the program to be versatile by being able to select different array types to be displayed?

the line:
Array<T>& Array<T>::operator=(const Array &rhs)
is there to enable the seleted array to be passed into a template reference? Is this the only way to do it?

template <class T >
class Array;

template <class T>
ostream& operator << (ostream& , Array<T>& );
template <class T> //declare the template and the parameter
class Array       // the class being paramterized


{
    public:
        //costrucyors
        Array(int itsSize = DefaultSize);
        Array(const Array &rhs);
        ~Array() {delete [] ptype;}

        //operators
        Array& operator = (const Array&);
        T& operator [] (int offset) {return ptype[offset];}
        const T& operator[] (int offset) const
        {return ptype[offset];}
        //accessors
        int GetSize() const {return itsSize;}

        friend ostream& operator << <> (ostream&, Array<T>&);



    private :
        T *ptype;
        int itsSize;
};


template <class T >
ostream& operator << (ostream& output, Array<T>& theArray)


{
    for (int i = 0; i < theArray.itsSize; i++)
      output << "[" << i << "]" << theArray[i] <<endl;

    return output;
}

//implementations follow...

//implement the constructor
template <class T>
Array<T>::Array(int size):
itsSize(size)
{
    ptype = new T[size];
    //the constructors of the type created
    //should be of default value
}

//copy constructor
template <class T>
Array<T>::Array(const Array &rhs)
{
    itsSize =rhs.GetSize();
    ptype = new T[itsSize];
    for (int i=0; i<itsSize; i++)
        ptype[i] = rhs[i];
}
//operator=

template <class T>
Array<T>& Array<T>::operator=(const Array &rhs)
{
    if (this == &rhs)
        return *this;
    delete [] ptype;
    itsSize = rhs.GetSize();
    ptype = new T[itsSize];
    for (int i =0; i<itsSize; i++)
        ptype[i] =rhs[i];
    return *this;
}
2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by Ravalon
0

I'm just trying to get a handle on the when and why of overloaded operators.

Cool. :)

the line: T& operator[] ... is there for use in the copy constructor?

Nah, the copy constructor doesn't invoke the overloaded operator at all, it uses the [] operator for pointers.

the line : const T& operator [] ... is there for the constructor definition?

Same thing sort of, the constructor uses the new[] operator for allocating an array of Ts. It's not the same thing as the operator[] that you defined for the class.

this is what enables the program to be versatile by being able to select different array types to be displayed?

I think so, if you mean how T works as a placeholder for some type to be defined later.

the line:
Array<T>& Array<T>:: operator=(const Array &rhs)
is there to enable the seleted array to be passed into a template reference? Is this the only way to do it?

Eh?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.