2.
- cv 'this'.
, ++ C:
class A
{
public:
void f1 ();
void f2 () const;
private:
int i;
};
, C:
struct A
{
int i;
};
void f1 (A * const this);
void f2 (A const * const this);
, , , , , (*this). :
void A::f1 ()
{
i = 0;
(*this).i = 0;
}
- const, this const:
void A::f2 () const
{
(*this).i = 0;
}
-, , , const , . :
class A
{
public:
void f () const
{
*i = 0;
}
private:
int * i;
};
It doesn’t look right, but it’s actually normal. (*this).iis a constant, so you cannot change what it points to i, but iit is still a pointer to non const int, so we can change the value that it points to i.
source
share