I find myself having a hard time deciding when and how to use member functions within a class.
Situation A:
i have a private member 'int x', and a method setX(int n){ x = n; }.
within a private member 'do_something(void)' i need to change the value of x...do i use setX() or simply x = a_number; ?
Situation B: (this will make more since after looking at the code)
i have a class which evaluates regular expressions.
i have a private member 'vector<int> regex' and a constructor
'myclass(const string &)'. a private method 'convert(const string &)' which converts a string into a vector of integers. there is another private member called 'makeValid(???)' which ensures a regular expression is valid by adding various characters to it. (ex: closing a group with ')' ) ...and then another private method which is called 'prepare(const string &)'
should i pass regex into these functions and then return regex?
like so
Code:
myclass::myclass(void){ prepare(); }
void myclass::prepare(const string &source)
{
regex = convert(source);
regex = makeValid(regex);
//theres quite a few more function that will manipulate
//regex the same way makeValid does
}
or just let the methods take care of it
Code:
myclass::myclass(void){ prepare(); }
void myclass::prepare(const string &source)
{
convert(source); //accesses regex directly
makeValid(void); //ditto
//theres quite a few more function that will manipulate
//regex the same way makeValid does
}
i know this seems like a trivial question, but all my classes eventually turn into spaghetti no matter how i implement them