0

I don't know why below code failed to compile with error:

"no instance of constructor "cb::iterator::iterator" matches the argument list argument types are:(int, const cb)"

But the code compiles fine when i uncomment the second version of constructor! why compiler considers *this as const?

class cb
{
public:
    class iterator
    {
    public:
        iterator(int x, cb& c):cb_(c)  { x_ = x; }
        //iterator(int x, const cb& c) :cb_(c) { x_ = x; }

    private:
            int x_;
            //cb a;
            const cb& cb_;
    };

    iterator begin() const; 
};

cb::iterator cb::begin() const
{
    return iterator(1, *this);

}
Pradeep
  • 9,667
  • 13
  • 27
  • 34
Islam Abdeen
  • 539
  • 3
  • 10

1 Answers1

0

For a class X, the type of this pointer is X* const if a member function of X is declared as const. So the parameter of constructor in this case should to be const too.

Here is a full explanation:
'this' pointer in C++

Islam Abdeen
  • 539
  • 3
  • 10