Monday 15 August 2011

c++ - How to call a non-const operator? -



c++ - How to call a non-const operator? -

so i'm having problem trying phone call specific operator. in class, i'm given:

template <class object> const object& myvector<object>::operator[] (int index ) const { if (index < 0 || index >= msize) throw myexception(); homecoming mobjects[index]; }

and i'm given

template <class object> object& myvector<object>::operator[]( int index ){ if (index < 0 || index >= msize) throw myexception(); homecoming mobjects[index]; }

i want phone call sec 1 can modify value, compiler keeps telling me can't because i'm trying modify constant.

here's i'm trying utilize operator function:

template <class object> const object& matrix<object>::get(int r, int c) const{ myobject *row = & myvectorobject[r]; //error //snipped }

and maintain getting error: cannot convert const myobject * myobject *

the matrix<object>::get() method const, if access class attributes of matrix in method, of them considered const.

myobject *row = & myvectorobject[r] calls const object& myvector<object>::operator[] (int index ) const.

const object& myvector<object>::operator[] (int index ) const homecoming const object&.

so myvectorobject[r] of type const object& , & myvectorobject[r] of type const object *

you should write: const myobject *row = & myvectorobject[r];

c++ const

No comments:

Post a Comment