non const ref return type is bad?

I was recently told that a method should never return a non-const reference to a type like so:

1
2
3
4
  Foo& getFoo()
  {
    return m_foo;
  }


I can think of some situational reasons why not, but there may be reasons why you would want to, so surely such an attitude to this is overly simplistic?
> so surely such an attitude to this is overly simplistic?

Yes.

We would never be able to modify an element in-place in a sequence container written with such an attitude. For instance, every sequence modifying algorithm in the standard library requires mutable or output iterators.

What is important is that it should not be possible for the class invariant to be violated through the reference to non-const that was returned.
Yes, that was my reasoning too. I thought it was odd that this person (who claimed considerable experience over myself) would make such an arbitrary statement.
Topic archived. No new replies allowed.