-1

I have changed access to a class variables from being public to private,

then created getters & setters (100 variables)

sometime later, after that is completed,

how can I automate, accessing any of these variables directly to set/get without going through every single one of these 100 variable. sometime a variable is accessed almost everywhere throughout a big project, so it is a bit tough to do by hand.

I am using Eclipse IDE, c++ project

9
  • I don't think you can easily just change the protection of things and "Find and Replace" on them. There's not an easy way to do this to an existing part of a project. Commented Sep 23, 2013 at 18:52
  • 3
    Just a thought: if you have 100 data members, maybe you should refactor the container class and break it into smaller classes. Commented Sep 23, 2013 at 18:58
  • Hence the reason you don't let your data members be accessed publicly ... changing a public interface is a PITA. Commented Sep 23, 2013 at 18:58
  • Pulling out the gut of you classes with functions instead of direct variable access doesn't make your code OOP at all.. except if you really do something beside simple assignment/return. Commented Sep 23, 2013 at 19:03
  • 1
    @g-makulik 100 classes sounds just as wrong... Commented Sep 23, 2013 at 19:04

1 Answer 1

3

If your project is really large & you're retrofitting private member control into it, you may find it helpful to go an alternate route: Don't implement getters at all.... just implement the setters. Define a const public reference to each underlying private member variable, which serves as the "getter" for it. Any code that reads this data can remain unchanged; you only have to update the code that sets these variables, which may be a lot less painful to update. If you go this route, you'll have to initialize these public references in the constructors, like this:

class fred {
public:
    fred() : stone(_stone) {} // every constructor must initialize all public references like this
    void set_stone(int s) { _stone = s; }
    int get_stone() { return _stone; } // Instead of this (or, in ADDITION to this), do this....
    const int& stone; // const public reference to internal private data member
private:
    int _stone;
}

You can go with that solution permanently, or only until you ("someday") finish updating all of your getters. Personally for classes where the data types of their internal data is pretty obvious & won't change, I prefer this solution anyway, as code written with it is just easier to read. I wish that C++ had an operator classmember_setter<membername>() feature that would allow you to write a setter method that would look just like you were simply accessing a data member instead of calling a method on it; that would result in a whole lot neater code than you get with setter methods.

Sign up to request clarification or add additional context in comments.

1 Comment

I like this Idea, however I wish for an IDE automation, similar to generating getters and setters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.