I'm reading a book called Beginning Algorithms by Harris and Ross, which gives code examples in Java. As an academic exercise I am trying to convert the code examples to C++. In the interface below the part that throws me is the return of the 'Object' by the 'current' method.

Java

public interface Iterator {
	public void first();
	public void last();
	public boolean isDone();
	public void next();
	public void previous();
	public Object current() throws IteratorOutOfBoundsException;
}

I have turned this into the following abstract class. In particular I would like to know if my use of the template function is the best way of solving the return of the the unknown object.

C++

class Iterator
{
	
	public:
		virtual void first()=0;
		virtual void last()=0;
		virtual bool isDone()=0;
		virtual void next()=0;
		virtual void previous()=0;
	template <typename V>
		V current();
};

Thanks for the reply. I realize there are other possible designs for iterators but in this case, is what I have done the closest translation of the Java interface? In this design model the iterator creates the container. The problem as I saw it was how to return the unknown 'Object'.

The problem as I saw it was how to return the unknown 'Object'

The Java programmer's Object is the C or C++ programmers void*.
If a Java method returns an Object, you need to cast it to the correct type before you can do something useful with it.

The closest C++ equivalent would be:

#include <stdexcept>

struct Iterator
{
    virtual ~Iterator() throw() {} // required in C++ (for well-defined behaviour)

    virtual void first() throw() = 0 ;
    virtual void last() throw() = 0 ;
    virtual void next() throw() = 0 ;
    virtual void previous() throw() = 0 ;

    virtual bool isDone() const throw() =  0 ;

    virtual void* current() const throw(std::out_of_range) = 0 ;
};

Edited 5 Years Ago by vijayan121: n/a

Thanks for that reply. I did some research on void* etc and the derived class would then be using templates to cast the pointer. I also note that in 'The C++ Programming Language' Stroustrup describes the use of void* in templates. Would you agree that although the void* mimics the Java interface most closely, the best C++ base would use templates?

Would you agree that although the void* mimics the Java interface most closely, the best C++ base would use templates?

Yes.

Also, you may want to keep this in mind. The way iterators are thought about in Java is fundamentally different from the way we use iterators in C++. Both privide an abstract mechanism to access the elements of a sequence one by one, but they use different concepts. A Java Iterator is an interface to a container; a single Java iterator knows about the entire range it traverses. A C++ iterator is an abstraction to identify one element of a sequence; it can move forward in the sequence, but has no idea about the range. In C++, we use a pair of iterators to denote a range.

Another (conceptually less important) difference is that a Java Iterator 'points' to the 'empty space' between elements of a sequence, while a C++ iterator directly 'points' to an element. The C++ iterator can be used like a pointer to an object.

A second area where the differences between the two languages is large is in resource management and error handling. Java uses the try-finally construct whereas C++ uses RAII proper.

I did some research on void* etc and the derived class would then be using templates to cast the pointer. I also note that in 'The C++ Programming Language' Stroustrup describes the use of void* in templates.

AFAIK, the use of a template specialization for void* as a base class is used to prevent code bloat when
a. A program uses instantiations of a template class for many different pointer types.
and b. The implementation of the template class is identical (except for the type) for all these pointer types.
See: http://linuxtopia.org/online_books/programming_books/c++_practical_programming/c++_practical_programming_122.html

In C++ generic programming, 'type erasure' ("the process of turning a wide variety of types with a common interface into one type with that same interface") is typically used to provide commonality across different types which can all be used in a similar fashion.
See: http://www.artima.com/cppsource/type_erasure.html

Thanks very much for the reply and the links. That's given me a few things to study.

AFAIK, the use of a template specialization for void* as a base class is used to prevent code bloat

Yes, Stroustrup's example that I read was for a base class Vector<void*> and he brings in the idea of a partial specialization at that point.

As far as the book 'Beginning Algorithms' is concerned I've managed to create a derived ArrayIterator and ReverseIterator class and have built tests for them using cppunit in a similar style to the junit tests shown in the book.

This question has already been answered. Start a new discussion instead.