Dinesh
Dinesh

Reputation: 1866

C++ programming, Iterator is a pointer even it is not declared a pointer

Consider the following lines

std::map<char,int> mymap;
std::map<char,int>::iterator it; /* not std::map<char,int>::iterator *it; */

In the second line even thought it is not declared as a pointer how are the elements are accessed using the arrow operator ( -> ) like bellow

std::cout << it->first << " => " << it->second << '\n';

Upvotes: 0

Views: 85

Answers (1)

Spook
Spook

Reputation: 25927

You can overload -> operator for your class and this exactly what happens here.

Another example:

class Hello
{
public:
    void Show()
    {
        printf("Hello, world!");
    }
};

class MyClass
{
private:
    Hello hello;

public:

    Hello * operator -> ()
    {
        return &hello;
    }
};


int main(int argc, char * argv[])
{
    MyClass m;

    m->Show();
}

Upvotes: 4

Related Questions