Jump to content
  • Advertisement
Sign in to follow this  
y2jsave

iterator doubt!

This topic is 2700 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello folks
Iam using c++ in my development ..
now i have a class inside which there is a function which return the start iterator of the list inside the class .

so its something like this ..

class myclass
{
list<int>* mylist;
... other stuff .
public :
list<int>::iterator getstartIter();
};

list<int>::iterator myclass::getstartIter()
{
if( mylist)
return mylist->begin();

else
// return what ????????? that i cant understand ..
//some folks say we should not return a NULL as it will work on linux but crash on windows :(
}

i hope i made my problem clear ..

thanks in advance
y2jsave

Share this post


Link to post
Share on other sites
Advertisement
This has nothing specific to iterators, but comes from your use of a pointer.
Linux or Windows, returning NULL will not crash anywhere. But trying to dereference NULL later will crash everywhere.

If mylist is not optional, don't make it a pointer (use a reference, or store it by value, depending on who owns it).
If it is optional, you have to decide yourself what getstartIter() is supposed to do when there is no list.

Share this post


Link to post
Share on other sites
Perhaps use assert, or throw an exception. The function can't really indicate if it can fail or not so your probably stuck with throwing an exception because of the way you've designed it.

I would add a new member:

bool HasList() const { return mylist != NULL; }

and throw an exception if someone tries to get the iterator when no list exists

list<int>::iterator myclass::getstarterItr()
{
if(!mylist)
{
throw std::exception("list not yet created");
}
return mylist->begin();
}

Then at last a user can do:

myclass A;
if(!A->HasList())
{
cout << "no list";
}
else
{
list<int>::iterator it = A.getstarterIter();
}


Its unclear what your class is supposed to do, but if its possible to use the iterator indirectly that may be better. Each time the list changes, the iterator is no longer valid (it may be for some containers, not sure about list) so any iterator an external body holds could fail without knowing. So rather than return the iterator, just access everyhting indirectly, hard to make a further suggestion as your class' purpose is unclear. Similaly, you could have an iterator belong to the class and return a pointer to it (rather than a copy), that way you can return NULL to indicate failure.

Share this post


Link to post
Share on other sites
Returning NULL seems simply wrong, since you have absolutely no guarantee that NULL can be casted to/is a valid value for list::iterator. For that you'd have to return a pointer to an iterator...

Share this post


Link to post
Share on other sites
i tried taking address of begin .. but code does not compile ..


#include<iostream>
#include<list>
using namespace std;

list<int>::iterator* getstart(list<int>* ll)
{
if(ll)
return &(ll->begin());
else
return NULL;
}

int main()
{

list<int>* ll = new list<int>;
ll->push_back(1);ll->push_back(2);
cout<<*(getstart(ll));

return 0;
}

errors
Compiler: Default compiler
Executing g++.exe...

warning: taking address of temporary
: In function `int main()':
:18: error: no match for 'operator<<' in 'std::cout << *getstart(ll)'
note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:74: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:86: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base&(*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:121: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:155: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:98: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/ostream:178: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/ostream:189: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/ostream:193: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/ostream:204: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:179: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:214: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:238: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/ostream:219: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:261: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>]

C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:284: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:307: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/ostream:449: note: std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:505: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char) [with _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/ostream:460: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char) [with _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/ostream:465: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char) [with _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:567: note: std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/bits/ostream.tcc:612: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*) [with _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/ostream:499: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*) [with _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/ostream:504: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*) [with _Traits = std::char_traits<char>]

Execution terminated

Share this post


Link to post
Share on other sites
i tried taking address of begin .. but code does not compile ..
The code that doens't compile is the part where you're trying to print an iterator object (instead of the value that the iterator points to).

I assume you want:list<int>::iterator* start = getstart(ll);
if( start )
cout<<**start;
else
cout<<"NULL";
or cout<<getstart(ll);

Share this post


Link to post
Share on other sites
Is there any particular reason you're storing a pointer to a list<int>, rather than just storing a list<int>?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!