C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[Project] List & ConstIterator

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
hokus
Guest





PostPosted: Thu Sep 29, 2005 8:59 am    Post subject: [Project] List & ConstIterator Reply with 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

Back to top
Shezan Baig
Guest





PostPosted: Thu Sep 29, 2005 12:24 pm    Post subject: Re: List & ConstIterator Reply with quote



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





PostPosted: Thu Sep 29, 2005 2:10 pm    Post subject: Re: List & ConstIterator Reply with 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

Back to top
hokus
Guest





PostPosted: Thu Sep 29, 2005 2:10 pm    Post subject: Re: List & ConstIterator Reply with 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

Back to top
Shezan Baig
Guest





PostPosted: Thu Sep 29, 2005 2:42 pm    Post subject: Re: List & ConstIterator Reply with quote


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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.