Re: java guy struggling with C++
* nthali:
I switched from C++ to java about 5 years ago, and now i'm back doing
some C++.
here's briefly my problem:
I took the C++ stl list class, and tried to wrap it with a java style
List class, and did the same with the iterator. so my iterator looks
like this:
template <class T>
class Iterator
{
public:
Iterator( List<T> myList )
{
this->myList = myList._list;
started = false;
}
bool hasNext()
{
if ( myList.size() == 0 ) return false;
// for some reason unknown to me, i had to put the .begin()
and .end() check in the same method
// otherwise i get a core dumped.
if ( started == false ) { iter = myList.begin(); started =
true; }
return iter != myList.end();
}
T next()
{
return *iter++;
}
private:
bool started; // why the heck do i have to do this?
list<T> myList;
list<T>::iterator iter;
};
template <class T>
class List
{
friend class Iterator;
public:
void add(T obj);
T* get(int i);
int size();
Iterator<T> iterator();
T* toArray();
private:
list<T> _list;
};
The problem i have is: the next() method in the iterator doesn't really
give me the original object in the list, so changing the values within
that object don't change the original object.
Pass by reference.
Anyone have any ideas on how to fix this?
The best advice is to /not/ try to wrap C++ code in Java-like syntax.
It's inefficient, complex (a.k.a. hard to grok) and error-prone.
But if you absolutely must, here's your code reworked so it compiles.
It has many shortcomings compared to direct use of the standard library
classes. For one thing, you should add more const accessors, and have a
const version of your Iterator class (or rather, drop the whole idea!).
#include <cstddef> // EXIT_FAILURE, EXIT_SUCCESS
#include <iostream> // std::cout
#include <list> // std::list
#include <ostream> // operator<<, std::endl
#include <stdexcept> // std::runtime_error, std::exception
#include <vector> // std::vector
bool throwX( char const s[] ) { throw std::runtime_error( s ); }
template< typename R, typename A >
R valueAs( A value )
{
// minR <= value && value <= maxR || throwX( "Gurgle..." );
return static_cast<R>( A );
}
template< class T >
class List
{
template< class T > friend class Iterator;
public:
typedef std::vector<T> Array;
void add( T const& obj ) { myList.push_back( obj ); }
T& at( int i ) { return myList.at( i ); }
T const& at( int i ) const { return myList.at( i ); }
int size() { return valueAs<int>( myList.size() ); }
Iterator<T> iterator();
Array toArray()
{
return Array( myList.begin(), myList.end() );
}
private:
std::list<T>& listMember() { return myList; }
std::list<T> myList;
};
template< class T >
class Iterator
{
public:
Iterator( List<T>& list )
: myEnd( list.listMember().end() )
, myIter( list.listMember().begin() )
{}
bool hasNext() const { return myIter != myEnd; }
T& next() { return *myIter++; }
private:
typename std::list<T>::iterator myEnd;
typename std::list<T>::iterator myIter;
};
template< class T >
Iterator<T> List<T>::iterator()
{
return Iterator<T>( *this );
}
void cppMain()
{
List<int> list;
list.add( 1 ); list.add( 2 ); list.add( 3 );
for( Iterator<int> it = list.iterator(); it.hasNext(); )
{
std::cout << it.next() << std::endl;
}
}
int main()
{
try
{
cppMain();
return EXIT_SUCCESS;
}
catch( std::exception const& x )
{
std::cerr << "!" << x.what() << std::endl;
return EXIT_FAILURE;
}
}
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]