Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Syntactical issue: should STL list Iterator be overloaded to a pointer to the current element?

Reply
Thread Tools

Syntactical issue: should STL list Iterator be overloaded to a pointer to the current element?

 
 
Olumide
Guest
Posts: n/a
 
      09-11-2007
Hello,

To begin with, I'm no STL expert. I've only just begun to use and
appreciate the power of STL.

Okay, so I have a list:

list<int> myLuckyNumbers;
list<int>::iterator intList_Iterator = myLuckyNumbers->begin();

while( intList_Iterator != myLuckyNumbers->end() )
{
int *integerPointer = &(*intList_Iterator);

intList_Iterator++;
}


In order to get a pointer to each element of the list, I need to use
the unintuitive expression, &(*intList_Iterator). My worry is that
because the dereference (*) and "address of" (&) operators, cancel
each other, shouldn't intList_Iterator be interpreted as a pointer to
a int, if assigned to an int?

Also, is there a better way getting the pointer to the current in in
the list?

Thanks,

- Olumide

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-11-2007
Olumide wrote:
> To begin with, I'm no STL expert. I've only just begun to use and
> appreciate the power of STL.
>
> Okay, so I have a list:
>
> list<int> myLuckyNumbers;
> list<int>::iterator intList_Iterator = myLuckyNumbers->begin();
>
> while( intList_Iterator != myLuckyNumbers->end() )
> {
> int *integerPointer = &(*intList_Iterator);
>
> intList_Iterator++;
> }
>
>
> In order to get a pointer to each element of the list, I need to use
> the unintuitive expression, &(*intList_Iterator). My worry is that
> because the dereference (*) and "address of" (&) operators, cancel
> each other,


It's a misconception. The dereference operator does not cancel the
address-of operator. They can (and the dereference is) overloaded
for the iterator object. No canceling occurs.

> shouldn't intList_Iterator be interpreted as a pointer to
> a int, if assigned to an int?
>
> Also, is there a better way getting the pointer to the current in in
> the list?


Better? In what way "better"?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Olumide
Guest
Posts: n/a
 
      09-11-2007
On 11 Sep, 21:11, "Victor Bazarov" <(E-Mail Removed)> wrote:
> Better? In what way "better"?


Better as in other than &(*intList_Iterator) .


 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      09-11-2007
"Olumide" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> On 11 Sep, 21:11, "Victor Bazarov" <(E-Mail Removed)> wrote:
>> Better? In what way "better"?

>
> Better as in other than &(*intList_Iterator) .


&(*iterator)
is the common method used to get the address of what iterator points to.
After you see it a few times it no longer looks weird.


 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      09-11-2007
On 2007-09-11 16:36:01 -0400, "Jim Langston" <(E-Mail Removed)> said:

> "Olumide" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>> On 11 Sep, 21:11, "Victor Bazarov" <(E-Mail Removed)> wrote:
>>> Better? In what way "better"?

>>
>> Better as in other than &(*intList_Iterator) .

>
> &(*iterator)
> is the common method used to get the address of what iterator points to.
> After you see it a few times it no longer looks weird.


It looks weird that way. The parentheses aren't needed. <g>

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

 
Reply With Quote
 
Olumide
Guest
Posts: n/a
 
      09-11-2007
On 11 Sep, 21:49, Pete Becker <(E-Mail Removed)> wrote:
> On 2007-09-11 16:36:01 -0400, "Jim Langston" <(E-Mail Removed)> said:
>
> > "Olumide" <(E-Mail Removed)> wrote in message
> >news:(E-Mail Removed) roups.com...
> >> On 11 Sep, 21:11, "Victor Bazarov" <(E-Mail Removed)> wrote:
> >>> Better? In what way "better"?

>
> >> Better as in other than &(*intList_Iterator) .

>
> > &(*iterator)
> > is the common method used to get the address of what iterator points to.
> > After you see it a few times it no longer looks weird.

>
> It looks weird that way. The parentheses aren't needed. <g>


&*iterator looks mucho weird .

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      09-11-2007
On 2007-09-11 16:55:14 -0400, Olumide <(E-Mail Removed)> said:

> On 11 Sep, 21:49, Pete Becker <(E-Mail Removed)> wrote:
>> On 2007-09-11 16:36:01 -0400, "Jim Langston" <(E-Mail Removed)> said:
>>
>>> "Olumide" <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed) oups.com...
>>>> On 11 Sep, 21:11, "Victor Bazarov" <(E-Mail Removed)> wrote:
>>>>> Better? In what way "better"?

>>
>>>> Better as in other than &(*intList_Iterator) .

>>
>>> &(*iterator)
>>> is the common method used to get the address of what iterator points to.
>>> After you see it a few times it no longer looks weird.

>>
>> It looks weird that way. The parentheses aren't needed. <g>

>
> &*iterator looks mucho weird .


Not at all, unless you don't know the language well enough to understand it.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      09-11-2007
Olumide wrote:
:: On 11 Sep, 21:49, Pete Becker <(E-Mail Removed)> wrote:
::: On 2007-09-11 16:36:01 -0400, "Jim Langston"
::: <(E-Mail Removed)> said:
:::
:::: "Olumide" <(E-Mail Removed)> wrote in message
:::: news:(E-Mail Removed) oups.com...
::::: On 11 Sep, 21:11, "Victor Bazarov" <(E-Mail Removed)>
::::: wrote:
:::::: Better? In what way "better"?
:::
::::: Better as in other than &(*intList_Iterator) .
:::
:::: &(*iterator)
:::: is the common method used to get the address of what iterator
:::: points to. After you see it a few times it no longer looks weird.
:::
::: It looks weird that way. The parentheses aren't needed. <g>
::
:: &*iterator looks mucho weird .

Not really, if you think about it:

*iterator

gives you access to an object, so

&*iterator

is the address of that object.


You will hardly ever have to do this anyway, as in C++ code there is
not much use for a pointer, if you already have an interator.

Probably it is mostly used in interfacing to old C code APIs, which in
C++ looks mucho weirdo anyway.


Bo Persson


 
Reply With Quote
 
Olumide
Guest
Posts: n/a
 
      09-11-2007
On 11 Sep, 22:54, "Bo Persson" <(E-Mail Removed)> wrote:
> Olumide wrote:
> :: &*iterator looks mucho weird .
>
> Not really, if you think about it:
>
> *iterator
>
> gives you access to an object, so
>
> &*iterator
>
> is the address of that object.


Thanks. I know unary operators are right associative ... Its just that
dereferencing and then taking the address of a variable *plain* C++ is
horribly redundant redundant.

 
Reply With Quote
 
Tristan Wibberley
Guest
Posts: n/a
 
      09-11-2007
On Tue, 2007-09-11 at 13:03 -0700, Olumide wrote:
> Hello,
>
> To begin with, I'm no STL expert. I've only just begun to use and
> appreciate the power of STL.
>
> Okay, so I have a list:
>
> list<int> myLuckyNumbers;
> list<int>::iterator intList_Iterator = myLuckyNumbers->begin();
>
> while( intList_Iterator != myLuckyNumbers->end() )
> {
> int *integerPointer = &(*intList_Iterator);
>
> intList_Iterator++;
> }
>
>
> In order to get a pointer to each element of the list, I need to use
> the unintuitive expression, &(*intList_Iterator). My worry is that
> because the dereference (*) and "address of" (&) operators, cancel
> each other, shouldn't intList_Iterator be interpreted as a pointer to
> a int, if assigned to an int?
>
> Also, is there a better way getting the pointer to the current in in
> the list?


You could try:

template<typename T>
T* iter_pointer(T* iter)
{
return iter;
}

template<typename T>
typename std::iterator_traits<T>:ointer iter_pointer(const T& iter)
{
return iter.operator->();
}

....

iterator_traits<list<int>::iterator>:ointer integerPointer
= iter_pointer(intList_Iterator);

This neatly avoids going through *intList_Iterator so iterators whose
pointer types are smart and reference count and stuff will work without
undefined behaviour until C++ finally supports generic smart references.

Unfortunately I don't know how to cope with the C++0x proposal to allow
nonmember operator->

--
Tristan Wibberley

Any opinion expressed is mine (or else I'm playing devils advocate for
the sake of a good argument). My employer had nothing to do with this
communication.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
List iterator assignment fails, assert iterator not dereferencable David Bilsby C++ 5 10-09-2007 02:05 PM
Using an iterator to remove an item from a STL list Jonathan C++ 6 03-14-2006 07:16 PM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
item callback on container or why STL does not have pointer to iterator conversion open? forester C++ 16 08-25-2005 03:37 PM
Hashes and Blocks (Syntactical/Feature question) Pete Elmore Ruby 6 11-04-2004 10:14 AM



Advertisments