What is the C++ equivalent of C#'s readonly field modifier?
class Foo
{
private:
const string _foo;
public:
Foo() : _foo("Unchangeable")
{
}
void ChangeIt()
{
_foo = "Darn"; // compiler error
}
};
That would be const. Note that this keyword means a couple of different things in different contexts.
There is no such thing directly. You can use a private field with a public getter (but no setter). But that would only apply to other classes calling your code. Foo
always has full acces to its members. But since you are the implementer of Foo
, this is no real problem.