Newbie question about const

Hi,

I am newbie in VC++ and come across something that I dont quite understand but it should be pretty simple.

During function prototyping what difference does the keyword const make

for example
DWORD GetPlatformID() const;
versus
DWORD GetPlatformID();

Thanks a bunch



Answer this question

Newbie question about const

  • David Vertex Harris

    Hi Brian,

    Thanks. This is a good one.
    For reference of others, more information can be obtained here

    http://msdn2.microsoft.com/en-us/library/4h2h0ktk(VS.80,d=ide).aspx



  • eebrown

    Thanks a lot for such a quick response.

    What exactly do you mean when you say

    "won't change an instance of the class"

    Is there a function that could change instance of the class If so, how could it be done



  • nathan.wells

    An exception to the rule is when a member is declared as mutable.  Data members marked as mutable can be modified by a const method.
  • Krishnareddym

  • Brian Teutsch

    Change the internal state of the object, like changing the value of one of its member variables.



  • mattpdk

    It tells the compiler that the GetPlatformID() method won't change an instance of the class. That is important if you ever want to call this method on an object that is const. Simple example:

    class demo {
    public:
    void method1() const;
    void method2();
    };
    void testconst(const demo& obj) {
    obj.method1();
    obj.method2(); // Error C2662
    }



  • Fran M.

    Thanks a lot !

    I get it now.



  • Chandrashekhar K

    Hi Brian,

    Thanks. This is a good one.




  • Newbie question about const