Automatic public getter setter generation in a TObject inherited class

Hello!

I am just wondering if there is a way to implement automatically public getter and setter methods inside our ROOT-ified classes.

We are lazy while implementing our c++ classes :frowning:

Perhaps this feature already exists and we are not aware of. Anyhow, it would be great if we could add a modifier (e.g {get/set}) at each data member that triggers the get/set methods implementation at the dictionary generation stage.

For example, if we would write something like

Int_t fDummyMember = 0; //< {get/set}

Then at the dictionary generation stage it would automatically generate

Int_t GetDummyMember() { return fDummyMember; }
void SetDummyMember( const Int_t &dummyMember ) { fDummyMember = dummyMember; }

I add @lobis in CC :slight_smile:

If the get and setter are that simply, did you consider “just” making the member public?

This feature does not exist as far as I know.

If you really like to use the Get/Set formalism over the public-member solution that pcanal suggested, for consistency against other classes or methods, one workaround would be to just use a preprocessor macro, see c++ - getter/setter generation with multiline stringfy macro - Stack Overflow

This way, you avoid relying on ‘undefined’ meta-behavior of dictionary generation (some user in the future might remove things thinking it’s just a comment, but it’s actually changing code). Which happened to me several times when using ROOT GUI Classes, where comments, exclamation marks, etc., play a role in signals, transients, context menu, ClassDef titles, etc.
Which is not the most robust programming choice in my opinion.

Yes @pcanal we though about making them public, but as @ferhue pointed out, I think we feel more comfortable using the Set/Get formalism.

Thanks for the insights!