Since you tagged this as C++, I think you mean const
method like this:
class A {
int e;
public:
int doSomething() const {
// ++ e; // Compiler error, change data-member in read-only structure
return e+1; // OK.
}
};
(Although C++'s const is not a true-const because of the mutable
members.)
Then I'm only aware of C++, D2, and all those functional languages supporting this.
- C# doesn't support
const
methods but you can make all members readonly
. You can also make a readonly
wrapper class/subclass. Java doesn't have the const keyword, but like C# you can make all members final
.
- All functional languages use const correct methods by default because the functions are pure, but whether they support Object-oriented programming is another question.