[Project] List & ConstIterator

H

hokus

I have the following class:

<CODE>

class List
{
public:

class Node
{
public:
Node* m_next;
};

class Iterator
{
public:
Node *m_position;
};

class ConstIterator
{
public:
ConstIterator( Node* pos );
Node *m_position;
};

// utility functions
ConstIterator begin() const;
Iterator begin();

// data members
Node* m_head;

};

</CODE>

The problematic implementation of const begin is:

<CODE>

List::ConstIterator
List::begin() const
{
return List::ConstIterator(m_head);
}

</CODE>

This is wrong because it is impossible to convert const List::m_head to just
pointer. My question is how to project class List to use Iterator and
ConstIterator functionality? I know I can use const_cast to remove constant
property from const List::m_head pointer but I think this isn't a good idea.

Thanks
 
M

Martin Ambuhl

hokus said:
I have the following class:

<CODE>

class List
{
public:

[etc.]
If you have a question about C++, ask in < In
<questions about such a byzantine language are frowned
on and not answered. If someone should post an answer, treat it with
all the respect you would give an answer from an APL, LISP, Fortran, or
Forth programmer about C++; that is to say, none.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top