Hi all,

for the code shown below , every thing is okey except few things i output the obj[i](sure the [] is overloaded) with the overloaded << but inside the operator definition i was outputing the m data member of the reference object,how ever it also outputs the m data member of obj[i] with the right data , i really wasn't expecting that i thought it will give me an error but how it gives me the correct behavior even the implementation of the function associated with m private varible and not n[inedx]

cout<<obj //give correct answer
cout<<obj[i] //how it give the correct answer?

////////////program code/////////////////

 #include<iostream>
    #include <cassert>
    using namespace std;
    class test{
    private:
        int n[10];
        int m;
    public:
        test(){
            m = 0;
            for (int i = 0; i < 10;i++)
            n[i] = 0;
        }
        test(int k) :m(k){}
        friend ostream&operator<<(ostream&, test&);
        friend istream&operator>>(istream&, test&);
        void operator++();
        test operator++(int);
        int&operator[](const int);

    };
    ostream&operator<<(ostream&out, test&c){
        out << c.m;
        return out;
    }
    istream&operator>>(istream&in, test&c){


                  in >> c.m;//in<<c.n; 

        return in;
    }

    void test::operator++(){
        ++m;

    }
    test test::operator++(int){
        test temp(m);
        ++(*this);
        return temp;
    }
    int& test ::operator[](const int index){

        assert(index >= 0 && index < 10);
        return n[index];
    }


    int main(){
        test obj(10);
        for (int i = 0; i < 10; i++) obj[i] = i*i;

        for (int i = 0; i < 10; i++)
                     cout << obj[i] << " ";
        cout << endl;
        system("pause");
        return 0;
    }

Not sure what you are trying to do in your class 'test' ...

Is it supposed to be a class for arrays of integers with a pre-fixed maximun size?

If that is what you are trying to code, this example may give you some ideas ?

// test_classMyArray.cpp //


#include <iostream>
#include <cassert>


using namespace std;

const int MAX_ARY_SIZE = 10;

class MyArray
{
public:
    MyArray() : len(0)
    {
        for( int i = 0; i < MAX_ARY_SIZE; ++i )
            ary[i] = 0;
    }
    MyArray( int size, int initVal )
    {
        assert( size <= MAX_ARY_SIZE );
        len = size;
        for( int i = 0; i < len; ++i )
            ary[i] =initVal;
    }

    void set_size( int size )
    {
        assert( size <= MAX_ARY_SIZE );
        len = size;
    }
    int size() const { return len; }

    typedef int* iterator;
    iterator begin() { return ary; }
    iterator end() { return ary+len; }

    typedef const int* const_iterator;
    const_iterator begin() const { return ary; }
    const_iterator end() const { return ary+len; }

    int& operator [] ( int );
    const int& operator [] ( int ) const;

private:
    int ary[MAX_ARY_SIZE];
    int len;

    friend ostream& operator << ( ostream&, const MyArray& );
} ;


ostream& operator << ( ostream& out, const MyArray& c )
{
    for( int i = 0; i < c.len; ++ i )
         out << c.ary[i] << ' ';
    return out;
}

int& MyArray::operator [] ( int index )
{
    assert( index >= 0 && index < MAX_ARY_SIZE );
    return ary[index];
}
const int& MyArray::operator [] ( int index ) const
{
    assert( index >= 0 && index < MAX_ARY_SIZE );
    return ary[index];
}




int main()
{
    const int size = 8, size2 = 5;
    MyArray ary( size, 0 ); // try this ctor...

    // get some values into ary ...
    for( int i = 0; i < ary.size(); ++i )
         ary[i] = i*i;

    cout << "showing ary ... method 1:\n";
    for( int i = 0; i < ary.size(); ++i )
         cout << ary[i] << " ";

    cout << "\nshowing ary ... method 2:\n"
         << ary;


    MyArray::const_iterator cit;

    cout << "\nshowing ary ... method 3:\n";
    for( cit = ary.begin(); cit != ary.end(); ++ cit )
         cout << *cit << ' ';


    MyArray ary2; // when try this ctor...
    ary2.set_size( size2 ); // then need to set size
    // get some values into ary ...
    for( int i = 0; i < ary2.size(); ++i )
         ary2[i] = i+10;

    cout << "\n\nshowing ary2 ... method 1:\n";
    for( int i = 0; i < ary2.size(); ++i )
         cout << ary2[i] << " ";

    cout << "\nshowing ary2 ... method 2:\n"
         << ary2 ;

    cout << "\nshowing ary2 ... method 3:\n";
    for( cit = ary2.begin(); cit != ary2.end(); ++ cit )
         cout << *cit << ' ';


    cout << "\n\nPress 'Enter' to continue/exit ... ";
    cin.get();
    return 0;
}

Edited 2 Years Ago by David W

This article has been dead for over six months. Start a new discussion instead.