| View previous topic :: View next topic |
| Author |
Message |
hokus Guest
|
Posted: Thu Sep 29, 2005 8:59 am Post subject: [Project] List & ConstIterator |
|
|
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
|
|
| Back to top |
|
 |
Shezan Baig Guest
|
Posted: Thu Sep 29, 2005 12:24 pm Post subject: Re: List & ConstIterator |
|
|
hokus wrote:
| Quote: | 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
|
The constructor for 'ConstIterator' should take a 'const Node*',
instead of just a 'Node*' (otherwise it wouldn't be a
'Const'Iterator!).
Hope this helps,
-shez-
|
|
| Back to top |
|
 |
hokus Guest
|
Posted: Thu Sep 29, 2005 2:10 pm Post subject: Re: List & ConstIterator |
|
|
This works of course, but how about std::list::const_iterator(_Nodeptr
_Pnode)? As you can see this function doesn't use const keyword. How to
accomplish this?
Thanks
|
|
| Back to top |
|
 |
hokus Guest
|
Posted: Thu Sep 29, 2005 2:10 pm Post subject: Re: List & ConstIterator |
|
|
This works of course, but how about std::list::const_iterator(_Nodeptr
_Pnode)? As you can see this function doesn't use const keyword. How to
accomplish this?
Thanks
|
|
| Back to top |
|
 |
Shezan Baig Guest
|
Posted: Thu Sep 29, 2005 2:42 pm Post subject: Re: List & ConstIterator |
|
|
hokus wrote:
| Quote: | This works of course, but how about std::list::const_iterator(_Nodeptr
_Pnode)? As you can see this function doesn't use const keyword. How to
accomplish this?
Thanks
|
That is an implementation detail of your STL vendor. They might
(probably) implement it using const_cast internally.
-shez-
|
|
| Back to top |
|
 |
|