What does const after the function name Primeter mean here in the below program ? I am not attaching the full code as it was somewhat big. I just want to understand meaning of const after function name
Please can you explain in a simple language through an example that what will happen if we do not use const ?
1) The compiler will not check that the method Sticker::Area()[b][/b] leaves the Sticker object unchanged. If you modify that method definition, and accidentally change it so that it changes the state of the object, then the compiler will not throw an error.
2) You will not be able to call the Area method on a non-const Sticker object - as in line 7 of keskiverto's example.
#include <iostream>
class MyClass {
int myprop {};
public:
// A const method can't modify class properties:
// int modMyprop1() const { return ++myprop; } // error: increment of member
// 'MyClass::myprop' in
// read-only object
int modMyprop2() const { return myprop + 1; } // fine - myprop is untouched
int modMyprop3() { return ++myprop; } // this is not const, so it's
// allowed to modify myprop;
// on the other hand, it can't
// be invoked by instances
// of MyClass which are
// declared 'const'
};
int main()
{
MyClass myinst1; // not const
// myinst1 can invoke every method (const and not const)
std::cout << "myinst1.modMyprop2(): " << myinst1.modMyprop2() << '\n';
std::cout << "myinst1.modMyprop3(): " << myinst1.modMyprop3() << '\n';
const MyClass myinst2;
// myinst2 can invoke only const methods
std::cout << "myinst2.modMyprop2(): " << myinst2.modMyprop2() << '\n';
// error: passing 'const MyClass' as 'this' argument discards qualifiers:
// std::cout << "myinst2.modMyprop3(): " << myinst2.modMyprop3() << '\n';
return 0;
}