C++ examples for Class:Member Function
If you declare a pointer to a const object, the only functions that you can call with that pointer are const functions.
#include <iostream> class Rectangle //from w w w .ja va 2s . co m { public: Rectangle(); ~Rectangle(); void SetLength(int length) { itsLength = length; } int GetLength() const { return itsLength; } void SetWidth(int width) { itsWidth = width; } int GetWidth() const { return itsWidth; } private: int itsLength; int itsWidth; }; Rectangle::Rectangle(): itsWidth(5), itsLength(10) {} Rectangle::~Rectangle() {} int main() { Rectangle* pRect = new Rectangle; const Rectangle *pConstRect = new Rectangle; Rectangle * const pConstPtr = new Rectangle; std::cout << "pRect width: " << pRect->GetWidth() << " feet\n"; std::cout << "pConstRect width: " << pConstRect->GetWidth() << " feet\n"; std::cout << "pConstPtr width: " << pConstPtr->GetWidth() << " feet\n"; pRect->SetWidth(10); // pConstRect->SetWidth(10); pConstPtr->SetWidth(10); std::cout << "pRect width: " << pRect->GetWidth() << " feet\n"; std::cout << "pConstRect width: " << pConstRect->GetWidth() << " feet\n"; std::cout << "pConstPtr width: " << pConstPtr->GetWidth() << " feet\n"; return 0; }