N
Nephi Immortal
I guess that you recommend to create two classes such as iterator
design. First version is const iterator and second version is non-
const iterator.
Why should you create two classes? You don’t need it. Declare one
template class. Put const in the template parameter. It does not
bother nothing, but it worked.
What do you think one template class below? Nothing is wrong.
Please comment.
template< class T >
class Test
{
public:
Test( T *_p ) : p( _p ), index( 0 )
{
}
~Test()
{
}
T &operator[]( int i )
{
return p[ i ];
}
/*
I commented this function below. You don't
need to declare const Test< T >. Remove
the function since you don't need it.
T operator[]( int i ) const
{
return p[ i ];
}
*/
private:
T *p;
int index;
};
int main()
{
char _data[] = "0123456789";
char tmp;
Test< const char > cT( _data );
Test< char > T( _data );
cT[ 0 ] = 2; // 'cT' : you cannot assign to a variable that is const
tmp = cT[ 0 ];
T[ 2 ] = 7;
tmp = cT[ 2 ];
return 0;
}
design. First version is const iterator and second version is non-
const iterator.
Why should you create two classes? You don’t need it. Declare one
template class. Put const in the template parameter. It does not
bother nothing, but it worked.
What do you think one template class below? Nothing is wrong.
Please comment.
template< class T >
class Test
{
public:
Test( T *_p ) : p( _p ), index( 0 )
{
}
~Test()
{
}
T &operator[]( int i )
{
return p[ i ];
}
/*
I commented this function below. You don't
need to declare const Test< T >. Remove
the function since you don't need it.
T operator[]( int i ) const
{
return p[ i ];
}
*/
private:
T *p;
int index;
};
int main()
{
char _data[] = "0123456789";
char tmp;
Test< const char > cT( _data );
Test< char > T( _data );
cT[ 0 ] = 2; // 'cT' : you cannot assign to a variable that is const
tmp = cT[ 0 ];
T[ 2 ] = 7;
tmp = cT[ 2 ];
return 0;
}