A comment to one of my posts interested me:
Me too. I also give accessors/mutators the same name.
I was wondering about this, because I have always used setBar(int bar)
instead of a mutator named the same thing. I want to know: can the compiler determine based on a const identifier what mutates at runtime, or can it use the same function name because it has a parameter?
Will this compile fine:
class Foo
{
int bar_;
public:
int bar() { return bar_; }
void bar(int bar) { bar_ = bar; }
}
Or do I have to do this (I realize I should be doing this anyways, just run with me on this):
int bar() const { return bar_; }
I don't know which is which. Const correctness is important, so I think I would want the compiler to object to the overloading since one mutates and one does not.
Why does it work this way?