CFanatic

Go Back   CFanatic > Programming > C++ Programming

Join CFanatic Forum Now

overloaded operators question topic posted under C++ Programming which is a part of Programming category in CFanatic Forum
Reply
 
Thread Tools Display Modes
  #1  
Old 12-31-2006, 01:19 AM
JRM JRM is offline
Junior Member
 
Join Date: Dec 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
JRM is on a distinguished road
| More
overloaded operators question

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?


Code:
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;
}
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
newby hashtable question DariusC C# Programming 2 09-08-2008 02:43 AM
question about so files ftom2 C++ Programming 1 08-18-2008 02:28 AM
C# debatable question MartijnMulder C# Programming 2 01-06-2007 12:42 AM
Beginners Question on 'return' MartijnMulder C# Programming 1 12-08-2006 02:33 PM
sorting question John C++ Programming 0 09-30-2006 04:57 AM

 

Advertisement

CFanatic Search
Custom Search

Advertisement

All times are GMT -5. The time now is 04:12 PM.



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2010, Crawlability, Inc.
Cfanatic.com is a premier member of the IDG TechNetwork. For advertising opportunities contact here
Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board