this pointer as a pointer to objects
suppose employee is a class....print is its non-static member function which prints t开发者_开发知识库he value of its private data member x...now i read that if print is a constant function the this pointer passed to it by the compiler is of the type
const employee* const
and if print is a non-constant function the type of this pointer is
employee* const
....now the problem is that i have not declared the object of class employee as constant so how can 'this' point to a constant employee object if print is declared a constant function....
Consider ice cream cones. Suppose you go to the ice cream shop and order a "vanilla cone." You'll get a cone with a scoop of vanilla ice cream. You can then add a cherry on top, but it's still what you ordered, though -- a vanilla cone.
Objects are kind of like the ice cream cone. The const-ness is like the cherry on top. You can add the constness to the object without changing the object itself:
class Foo
{
public:
void DoIt() {};
} foo;
Foo* regular_foo = &foo; // OK
const Foo* const_foo = &foo; // Also OK
Back to the ice cream shop. This time order a vanilla cone with sprinkles. They give you a cone with 1 scoop of vanilla ice cream and some candies sprinkled on top. Now you can't take the sprinkles off without changing what you ordered. If you take off the sprinkles you won't have a "vanilla cone with sprinkles", you'll just have a vanilla cone.
class Foo
{
public:
void DoIt() {};
};
const Foo foo;
Foo* regular_foo = &foo; // Not OK!
const Foo* const_foo = &foo; // But this is OK
EDIT:
For a more rigorous treatment of my analogy above, you can read the passage int he Standard quoted by @Potatoswatter:
4.4 Qualification conversions [conv.qual]
1 An rvalue of type “pointer to cv1 T” can be converted to an rvalue of type “pointer to cv2 T” if “cv2 T” is more cv-qualified than “cv1 T.”
const employee* const
is a const pointer to a const employee. Contrast that with employee* const
which is a const pointer to a non-const employee.
In the former case, neither this
nor anything it points to may be modified. In the latter case, this
may not be modified, but you may modify anything this
points to.
There is no problem in having a const employee*
pointing to a non const object. Having a employee*
pointing to a const object is problematic and prevented by the language rules (until you begin to play with const_cast).
how can 'this' point to a constant employee object if print is declared a constant function....
With the const
modifier at the end of the non-static member function print
, state of the object upon which it is called cannot be modified in it( how ever with const_cast
typecast it is possible and that voids very use of const
modifier for the member function ). That is what the significance of const
at the end of the function.
The const
is called a cv-qualifier; const employee *
is more cv-qualified than employee *
. §4.4/1 of the Standard says
A prvalue of type “pointer to cv1 T” can be converted to a prvalue of type “pointer to cv2 T” if “cv2 T” is more cv-qualified than “cv1 T”.
In plain English, const
simply guarantees that you won't try to change the object through that pointer. It is always fine to add that guarantee and restrict yourself further.
A problem occurs only when you try to eliminate such a guarantee, which is why you cannot use a non-const method (and obtain a non-const this
) from a const object.
Declaring a pointer as const T* p;
does not mean that p
points to a constant T
. It means that p
points to a T
, which can be either constant or non-constant, and which cannot be changed through p
.
精彩评论