1

Possible Duplicate:
What's the difference between a const member function and a non-const member function?

    class Message
{
    public:
        Message(const char* pStr, const char* key);
        Message(const char* pStr);
        Message();

        void encryptMessage();
        void decryptMessage();

        const char* getUnMessage() const;
        const char* getEnMessage() const;

        void getMessage();
        void getKey();

        ~Message();

    private:
        char* pUnMessage;
        char* pEnMessage;
        char* pKey;
};

In this program, why using const? (2 different places) Please explain those 2 for me. Thank you very much!

Community
  • 1
  • 1
Josh Morrison
  • 7,488
  • 25
  • 67
  • 86

2 Answers2

1

const is used in C++ for a lot of things. Typically to declare that a method doesn't actually modify the state of an object, or to declare that a pointer cannot be modified, or to declare that a value cannot be modified, or both.

For further reading, I suggest the following:

http://duramecho.com/ComputerInformation/WhyHowCppConst.html

Randolpho
  • 55,384
  • 17
  • 145
  • 179
0

const char* getUnMessage() const;

Indicates that getUnMessage() may be invoked for an instance of the class that is const. It doesn't have any effect on the const-ness of the return value.

const char* getUnMessage() const;

...indicates that the value being returned from getUnMessage() is const (and says nothing about the const-ness of the object having its member function called.)

Sudantha
  • 15,684
  • 43
  • 105
  • 161
  • 2
    Try "may be invoked for any instance of the class, even if it is const". If you don't specify `const`, it can't be called through a const instance, but when you do, it can be called for both const and non-const class instances. – Ben Voigt Dec 24 '10 at 04:57