Usage of mutator methods

Hi all!

I don't really know whether this is a "Beginner's question" but maybe it is ,..well, if so: sorry for that

Let's guess I have written a class which has private variables. In addition the class does have member functions which do require write/read access to those private variables.
I always thought that due to data encapsulation those variables should (always?) be accessed using mutator/accessor methods.

Now my question is:
Should I allow those member functions to gain direct write/read access to these private variables? Is it potentially more expensive to call the respective mutator/accessor methods even when they have been implicitly defined inline?

The reason for my question is, that sometimes code appears quite sloppy to me when using member variables within a member function in which they have not been declared. Well of course I could use "this" but that also sometimes seems to make code harder to read. So I wondered whether the use of mutator/accessor methods inside their own class is legitimite or even equal in performance when they are implicitly defined inline.

thanks :)
Personal preference. It is not more expensive if the methods are non-virtual and inline.

I'd use accessors/mutators if you intend the variables to be public or protected. If the variables are private and you would make the accessors/mutators private also, I would just access the variables directly instead of creating a zillion inline functions.
Topic archived. No new replies allowed.