Design one iterator class

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;
}
 
M

Marc

Nephi said:
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.

Yes, it is sometimes done that way.
What do you think one template class below? Nothing is wrong.
Please comment.

iterator needs to be convertible to const_iterator.
You are missing all the typedefs for iterator_traits.
Have a look at boost.
 
J

Joshua Maurice

        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.

You should learn about this thing called "const correctness" and how
"T * const x;" is different than "T const * x;".

"std::vector<int>::iterator x;" is like "int * x;".
"std::vector<int>::iterator const x;" is like "int * const x;".
"std::vector<int>::const_iterator x;" is like "int const * x;".
"std::vector<int>::const_iterator const x;" is like "int const * const
x;".

If you iterator does not allow modification, then sure, you don't need
both iterators types. However, if iterator does allow modification,
and you want const correctness, then you want both iterator types.
 
N

Nephi Immortal

You should learn about this thing called "const correctness" and how
"T * const x;" is different than "T const * x;".

"std::vector<int>::iterator x;" is like "int * x;".
"std::vector<int>::iterator const x;" is like "int * const x;".
"std::vector<int>::const_iterator x;" is like "int const * x;".
"std::vector<int>::const_iterator const x;" is like "int const * const
x;".

If you iterator does not allow modification, then sure, you don't need
both iterators types. However, if iterator does allow modification,
and you want const correctness, then you want both iterator types.

My understanding is that you can’t put char * const in the template
class because it can only recognize either char * or char const *, but
not char * const and char const * const.
If you try either char * const or char const * const, template
ignores it and sticks with char * and char const *.
 
M

Michael DOUBEZ

        My understanding is that you can’t put char * const in the template
class because it can only recognize either char * or char const *, but
not char * const and char const * const.
        If you try either char * const or char const * const, template
ignores it and sticks with char * and char const *.

Did you try it:
template<class U, class V>
struct is_same
{
static const bool value = false;
};
template<class T>
struct is_same<T,T>
{
static const bool value = true;
};

int main()
{
typedef Test<char * > CharPtr;
typedef Test<char const * > CharConstPtr;
typedef Test<char * const > CharPtrConst;
typedef Test<char const * const > CharConstPtrConst;

std::cout<< is_same<CharPtr , CharPtrConst >::value
<<'\n';
std::cout<< is_same<CharConstPtr, CharConstPtrConst>::value
<<'\n';
}

Result:
0
0
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top