![c++ prototype c++ prototype](https://images.slideplayer.com/39/10869197/slides/slide_93.jpg)
Which would translate to this->x = 100 which the compiler will complain about. For clarity I should point out that the class prototype, definition and functions are all in the same file and compile fine if I don’t add the prototype and the code that requires it.
![c++ prototype c++ prototype](http://idc.sourceforge.net/wiki/idc-factorial-3.png)
Since the type of the this pointer is const Foo*, you cannot change the object the pointer points to i.e. How is this achieved? When you declare a non-static member method of the class, the C++ compiler slyly changes the signature of the methods to expect a "this" pointer, that is the pointer to the object used to invoke the method.įor a non-const method, the type of the "this" pointer expected is Class*.
![c++ prototype c++ prototype](https://cdn.programiz.com/sites/tutorial2program/files/passing-arguments-cpp.jpg)
Further, all the non-static member methods need an object to invoke them. What does this mean? When you declare a class, there is a single version of each of the member functions which gets loaded onto memory. it is not expected to modify the object which is used to invoke them. In the above class definition, GetX() is a const method i.e. It has no relation with the return type of the method. Means that the method cannot modify any of the member variables of the object used to invoke the method. It tells that the method does not "mutate" or "modify" the object it operates on. an object that cannot be modified by the caller of this method.Ĭonst appearing after the method describes the attributes of the method itself. Means that the method returns a const CString object i.e. Const appearing before the method describes the attributes of the return type of the method.