1

I'm implementing a vector, so i wanted it to behave as an array. That's the reason i tried to implement the subscripting operator but i haven't achieve the correct behavior.

The implementation was something like this:

template <typename value_type>
class Vector{ 
    private:
    value_type ** vector ; 
    long size ; 
    long usedSize ; 

    public: 
    /.../ 
    value_type & operator [] (long) ; // For writing.
    const value_type & operator [] (long) const ; // For reading.
    /.../
}

template<typename value_type> 
value_type & Vector<value_type>::operator[] ( long index ) {
    if ( index < 0 || index > usedSize ) 
        return out_of_range () ; 
    else {
        vector[index] = new value_type () ; 
        usedSize++ ; 
        return *(vector[index]) ;
    }
} 

template<typename value_type> 
const value_type & Vector<value_type>::operator[] ( long index ) const {
    if ( index < 0 || index > usedSize ) 
        return out_of_range () ; 
    else { return (*vector[index]) ; }
}

Then i test the behaviour of the object with this:

int main (void) { 
    Vector<int> * v = new Vector ( 10 ) ; // Creates a vector of 10 elements.
    (*v)[0] = 3 ; 
    int a = (*v)[0] ; 
    cout << "a = " << a << endl ;
}

And i get this from the execution:

$> a = 0 

Some threads recommend using a handler class overloading the assignment operator, i wonder if there's anyway to avoid the use of a handler object to do the task.

Thanks in advance.

Gonzalo from Argentina.

4
  • If your usedSize is initialized to 0 then wouldn't any call to operator[] return out_of_range() instead of a place in your array? What does out_of_range() look like? Commented Jun 1, 2013 at 23:03
  • I'm a bit confused by cout << "a = " << 0 -- did you mean cout << "a = " << a? Commented Jun 1, 2013 at 23:05
  • @GuyGreer No! when ``usedSize = 0` and index = 0 the conditional statement its false, because index is equal to usedSize not greater. out_of_range its a standard c++ exception. See it here Commented Jun 1, 2013 at 23:13
  • @NateKohl Yes! you're right. In my implementation it's been written properly. That's not the problem of the object misbehavior. Commented Jun 1, 2013 at 23:17

1 Answer 1

1

You are wrong assuming that in line

cout << "a =" << (*v)[0] << endl;

The

const value_type & Vector::operator[] ( long index ) const

will be used.

In fact both times

value_type & Vector::operator[]

is used so you "replace" previous value with new (and leaking memory at the same time)

below should help

value_type & Vector<value_type>::operator[] ( long index ) {
    if ( index < 0 || index > usedSize )
        ///out of bounds handling
    else {
        if(vector[index]== 0)
        {
            vector[index] = new value_type () ;
            usedSize++ ;
        }
        return *(vector[index]) ;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I knew that the problem was that int a = (*v)[0] ; wasn't calling the "reading" function, but i didn't realize that the was a way to solve the problem just using one method. Thanks you very much for your help.
remember that return type is not a factor in overload selection. if you would make const Vector* in main - the other overload would be selected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.