1

As seen example below iit object initialization returns as pointer, can anyone explain how is returns as pointer from constructor?

int main() 
{

 std::istream_iterator<int> iit (std::cin);
 std::cout  << *iit;
 return 0;

}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Kad
  • 542
  • 1
  • 5
  • 18
  • 3
    You seem to misunderstand how *iterators in general* work. I recommend that you [get a couple of good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to read. – Some programmer dude Sep 24 '18 at 12:12
  • 1
    `iit` is an iterator (something what supposed to behave like a pointer). Please read your handbook about iterators. – Marek R Sep 24 '18 at 12:15
  • 1
    Possible duplicate of [Iterator = pointer? Or what is it?](https://stackoverflow.com/questions/30950285/iterator-pointer-or-what-is-it) – Jan Rüegg Sep 24 '18 at 12:45

2 Answers2

7

There are no pointers here. Seeing *iit in the code doesn't mean that iit is a pointer - C++ allows operator overloading on arbitrary operators.

std::istream_iterator<int> simply overloads unary operator*.

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
3

iit is not a pointer, and your *iit is a reference to a int. See, this is the declaration of std::istream_iterator::operator*:

const T& operator*() const;

Template class std::istream_iterator overloads the unary * operator, in which it "Returns a [...] reference to the current element".

You can read here about the different kinds of iterators, and get to understand the entire concept of it and fundamental part it has in C++.

Geezer
  • 5,600
  • 18
  • 31