Home > Enterprise >  return an iterator from a method
return an iterator from a method

Time:11-16

I am porting some old code. at moment I am stuck with C 98 standard. I have this example:

#include <iostream>
#include <vector>
#include <iterator>
using namespace std;

class ClassPointPrediction {

};
class ClassBuildPointPosition
{
    typedef vector<ClassPointPrediction> ClassPointPredictionContainer;
        const ClassPointPrediction* const currentPrediction() const;
private:
ClassPointPredictionContainer::iterator CurrentPoint;
};

const ClassPointPrediction* const ClassBuildPointPosition::currentPrediction() const
{

    return  CurrentPoint;
}

int main() {

cout<<"test\n";
    return 0;
}

ADDED: The usage of the method is:

const ClassPointPrediction*  currentPrediction = pointPositions->currentPrediction();

and of course the iterator is already initialised:

CurrentPoint = PointPredictions.begin();

Trying to compile I receive

In member function ‘const ClassPointPrediction* const ClassBuildPointPosition::currentPrediction() const’:
/tcenas/home/iarnone/workspace/predictionstar/main.cpp:33:13: error: cannot convert ‘std::vector<ClassPointPrediction>::iterator’ {aka ‘__gnu_cxx::__normal_iterator<ClassPointPrediction*, std::vector<ClassPointPrediction> >’} to ‘const ClassPointPrediction* const’ in return
     return  CurrentPoint;
             ^~~~~~~~~~~~

how to correclty return that iterator? should I initialize like this before return?

ClassBuildPointPosition::currentPrediction() const
    {
        const ClassPointPrediction* temp;
        return  CurrentPoint;
    }

CodePudding user response:

You've specified that the return type of the function is const ClassPointPrediction*. The type of the iterator is ClassPointPredictionContainer::iterator. The type of the iterator isn't (necessarily convertible to) const ClassPointPrediction*. In order to return an object of type ClassPointPredictionContainer::iterator, you should specify that the return type is ClassPointPredictionContainer::iterator, rather than specifying the return type being something else such as const ClassPointPrediction* for example.

That said, you may want to return ClassPointPredictionContainer::const_iterator instead. Like this:

ClassPointPredictionContainer::const_iterator currentPrediction() const;

ClassPointPredictionContainer::iterator CurrentPoint;

This variable is default initialised. If ClassPointPredictionContainer::iterator is trivially constructible - which it may be - then it has an indeterminate value. In such case, you must not read its value, such as for example as you do here:

return  CurrentPoint;

If you do this, the behaviour of the program will be undefined. To fix this, properly initialise the iterator that you return.

CodePudding user response:

This function

const ClassPointPrediction* const ClassBuildPointPosition::currentPrediction() const
{
    ClassPointPredictionContainer::iterator CurrentPoint;

    return  CurrentPoint;
}

in any case does not make a sense because it returns uninitialized variable.

Many early implementations of the iterator of the class template std::vector use ordinary pointers to elements of the vector as iterators.

You could rewrite the function for example the following way

auto ClassBuildPointPosition::currentPrediction() const
{
    ClassPointPredictionContainer::const_iterator CurrentPoint;

    return  CurrentPoint;
}

Or the following way

    ClassBuildPointPosition::ClassPointPredictionContainer::const_iterator
ClassBuildPointPosition::currentPrediction() const
{
    ClassPointPredictionContainer::const_iterator CurrentPoint;
    
    return  CurrentPoint;
}
  • Related