I just learned about returning a reference... and it seems to me that I can delete my mutators now? ie.
class foo
{
int A;
//mutator
void set_A(const int a) {A=a;}
//accessor
int get_A() {return A;}
}
int main()
{
foo MyFoo;
int b = MyFoo.get_A(); //get
MyFoo.set_A(4); //set
}
vs
class foo
{
int A;
//accessor AND mutator
int& a() {return A};
}
int main()
{
foo MyFoo;
int b= MyFoo.a(); //get
MyFoo.a() = 4; //set
}
Can anyone tell me when I should keep them separate and when I can combine them like this? Or do these do something different (ie. I am understanding it incorrectly) ?
Thanks!
Dave