C++ Array with iterators I wrote an Array class with iterators:

#include &lt;iostream&gt;
using namespace std;
template &lt;class T&gt;
class Array
{
    T* data;
    int size;
public:
    class mException { };
    Array(int nsize=1)try:data(new T[nsize]),size(nsize)
    {

    }
    catch(bad_alloc&amp; r)
    {
        cout&lt;&lt; "Cant allocate";
        throw;
    }
    Array(const Array&amp; rhs):data(new T[rhs.size]),size(rhs.size)
    {
        for(int i=0;i&lt;size;i++)
        {
            data[i] = rhs.data[i];
        }
    }
    Array&amp; operator=(const Array&amp; rhs)
    {
        if(this== &amp;rhs) return *this;
        delete [] data;
        size = rhs.size;
        data = new T[size];
        for(int i=0;i&lt;size;i++)
        {
            data[i] = rhs.data[i];
        }
        return *this;
    }
    T&amp; operator[](int index) throw (mException)
    {
        if(index&lt;0 || index&gt;=size) throw mException();
        return data[index];
    }
    const T&amp; operator[](int index) const throw(mException)
    {
        if(index&lt;0 || index&gt;=size) throw mException();
        return data[index];
    }
    const Array operator++(int) //postfix
    {
        Array temp = *this;
        ++*this;
        return temp;
    }
    Array&amp; operator++()
    {
        for(int i=0;i&lt;size;i++)
            data[i]++;
        return *this;
    }
    friend ostream&amp; operator&lt;&lt;(ostream&amp; out, const Array&amp; rhs)
    {
        out &lt;&lt; "(";
        for(int i=0;i&lt;rhs.size;i++)
        {
            out &lt;&lt; rhs.data[i];
            if(i&lt;rhs.size-1)
             out &lt;&lt;  " ";
        }
        out &lt;&lt; ")" &lt;&lt; endl;
        return out;
    }
    friend istream&amp; operator&gt;&gt;(istream&amp; in,Array&amp; rhs)
    {
        cout &lt;&lt; "enter " &lt;&lt; rhs.size &lt;&lt; " digits to fill the array" &lt;&lt; endl;
        for(int i=0;i&lt;rhs.size;i++) in &gt;&gt; rhs.data[i];
        return in;
    }

    ~Array()
    {
        delete [] data;
    }
    bool empty() const
    {
        return size==0;
    }
     T front()
    {
        return data[0];
    }
     T back()
    {
        return data[size-1];
    }
    class iterator
    {
        const Array* data;
        int index;
        friend class Array;
    public:
        iterator(const Array* arr,int size): data(arr),index(size) { }
        const T&amp; operator*() const
        {
            return data-&gt;data[index];
        }
        const iterator operator++(int)
        {
            iterator temp = *this;
            ++*this;
            return temp;
        }
        iterator&amp; operator++()
        {
            ++index;
            return *this;
        }
        friend bool operator==(const iterator&amp; rhs,const iterator&amp; lhs)
        {
            return !(rhs.index!=lhs.index);
        }
        friend bool operator!=(const iterator&amp; rhs, const iterator&amp; lhs)
        {
            return !(rhs.index==lhs.index);
        }
    };
    iterator begin() const{ return iterator(this,0); }
    iterator end() const{ return iterator(this,size); }
};


I have a few questions:

Should I throw exceptions in the iterator class?

I have exceptions in templates, and I have a catch for every type in main alone, and this could be a problem.
Is throwing exceptions from exception (like I did) a good choice?

I know this is not a complete version. This is for educational purposes and I need to know where I did badly.
Using using namespace std;

Don't. See here: Why is "using namespace std" considered bad practice.

Exception Handling

class mException { };


Please just throw the correct standard exception std::invalid_argument, std::runtime_error and so forth. At the very least, inherit from std::exception. You can find all of the standard exceptions here: cppreference.com.

Array(int nsize=1)try:data(new T[nsize]),size(nsize)
{

}
catch(bad_alloc&amp; r)
{
    cout&lt;&lt; "Cant allocate";
    throw;
}


I've never actually seen this type of try/catch used... If your allocation throws a bad_alloc you may be in an out of memory situation or the block was too large. At any rate it is not the job of your container class to print the message or handle the allocation failure, it is the user's job. Just let the exception fly. 

Use Copy &amp; Swap idiom for operator =

Copy and swap is an easy way to implement operator = with strong exception safety. You can read more about it here: What is the copy and swap idiom?.

Throw specification

You rarely need to specify the classes thrown here, just skip them. Also throw std::out_of_range instead.

To reduce code duplication you can implement the mutable (non-const) version using the const version and two const_casts (but not the other way around).

T&amp; operator[](int index) throw (mException)
{
    if(index&lt;0 || index&gt;=size) throw mException();
    return data[index];
}
const T&amp; operator[](int index) const throw(mException)
{
    if(index&lt;0 || index&gt;=size) throw mException();
    return data[index];
}


So this should be:

T&amp; operator[](int index){
    // Being pedagogical here so you see what's going on.
    // The below return statement is equivalent to this.
    // const Array* const_this = const_cast&lt;const Array*&gt;(this);
    // const T&amp; const_ref = (*const_this)[index];
    // return const_cast&lt;T&amp;&gt;(const_ref);
    return const_cast&lt;T&amp;&gt;((*const_cast&lt;const Array*&gt;(this))[index]);
}
const T&amp; operator[](int index) const
{
    if(index&lt;0 || index&gt;=size) throw std::out_of_range("Index out of range!");
    return data[index];
}


Note that it's important that the mutable version calls the const version. Not the other way around. 

If you have a mutable instance, then calling a const method is OK. As the operator returns a reference into your mutable instance (even though you're accessing it in a const manner), then casting away the constness is safe. 

But if you on the other hand call a mutable function from a const method, then the mutable function might modify your const data and this my friend, is no bueno.

Operator ++

The pre- and post- increment operators don't make sense to me. But I guess you need them for something.

Simplify your logic

This:

    for(int i=0;i&lt;rhs.size;i++)
    {
        out &lt;&lt; rhs.data[i];
        if(i&lt;rhs.size-1)
         out &lt;&lt;  " ";
    }


could be:

    for(int i = 0; i &lt; rhs.size - 1; i++) {
        out &lt;&lt; rhs.data[i] &lt;&lt;  " ";
    }
    if(rhs.size &gt; 0){
        out &lt;&lt; rhs.data[rhs.size-1];
    }


It avoids checking the condition inside of the loop each time. (Although your compiler might already do this).

Input stream operator

It is not the array's job to print the explanation of how to use the input operator, that's up to your user. So this:

friend istream&amp; operator&gt;&gt;(istream&amp; in,Array&amp; rhs)
{
    cout &lt;&lt; "enter " &lt;&lt; rhs.size &lt;&lt; " digits to fill the array" &lt;&lt; endl;
    for(int i=0;i&lt;rhs.size;i++) in &gt;&gt; rhs.data[i];
    return in;
}


Should simply be:

friend istream&amp; operator&gt;&gt;(istream&amp; in,Array&amp; rhs)
{
    for(int i=0;i&lt;rhs.size;i++) in &gt;&gt; rhs.data[i];
    return in;
}


As you are using istream it means that you can pass a ifstream pointing to a file on the disk to read from. And it's just silly to be forced to print the instructions each time you read from the file automatically.

Return by reference

Your front() and back() methods should return by reference. And you should also implement const versions of these.

Iterator implementation

A basic pointer satisfies the random access iterator criteria and has specialisations for std::iterator_traits. So you could basically just do this:

typedef T* iterator; 


and be done with it. But as this is a learning experience, lets look at your iterator code.

You are storing the base pointer and the index. You can simply store 1 pointer to the indexed element and increment and decrement the pointer instead, this reduces your code. Also you're missing the required typedefs for an iterator for example you're not specifying what iterator category it is so your iterator is not compatible with many standard library functions. You need these as std::iterator_traits is not specialised for your type. The easiest way to get the right typedefs is to inherit from std::iterator&lt;...&gt; and give the correct type arguments to the template. Note that this only gives you the type defs and no other functionality, you still need to implement the iterator.

You can read more about what requirements are put on iterators here: RandomAccessIterator and on the linked pages.

Also there is an easier way to implement the operator ==/!=:

    friend bool operator==(const iterator&amp; rhs,const iterator&amp; lhs)
    {
        return !(rhs.index!=lhs.index);
    }
    friend bool operator!=(const iterator&amp; rhs, const iterator&amp; lhs)
    {
        return !(rhs.index==lhs.index);
    }


should be:

    friend bool operator==(const iterator&amp; rhs,const iterator&amp; lhs)
    {
        return rhs.index == lhs.index;
    }
    friend bool operator!=(const iterator&amp; rhs, const iterator&amp; lhs)
    {
        return !(rhs == lhs);
    }


Now whenever operator == changes, operator != is automatically updated as it uses operator ==. Clever huh?

The whole thing rewritten with just one pointer should be:

class iterator{
    const T* data;
public:
    iterator(const T* arr): data(arr) { }
    const T&amp; operator*() const
    {
        return *data;
    }
    const iterator operator++(int)
    {
        iterator temp = *this;
        ++*this;
        return temp;
    }
    iterator&amp; operator++()
    {
        ++data;
        return *this;
    }
    friend bool operator==(const iterator&amp; rhs,const iterator&amp; lhs)
    {
        return !(rhs.index!=lhs.index);
    }
    friend bool operator!=(const iterator&amp; rhs, const iterator&amp; lhs)
    {
        return !(rhs.index==lhs.index);
    }
};

iterator begin() const{ return iterator(data); }
iterator end() const{ return iterator(data + size); }


Notice how the iterator class behaves like a restricted pointer? Yepp, you are correct, it basically just wraps a pointer in a pointer-like interface. So you could just as well have used a pointer (like I mentioned at the start, a pointer is a fully fledged random access iterator, I hope the above made it clearer as to why it's OK).

You're also missing operator -&gt; and a lot of other things but I choose to ignore that for now. 

Style

For crying out loud, white space is free. Use them! At least put spaces around all operators. For example this:

    for(int i=0;i&lt;size;i++)


should be 

    for(int i = 0; i &lt; size; i++)


for readability.