"#define private public" - Neat little trick to access private members

I had a class inside a class inside a class (all private members) whose variable I wanted to access easily for debugging reasons, with minimal modification to the code.

I found a really easy way to access the variable from my main function simply by using a preprocessor declaration that turns all privates public :wink:

Then from main I was able to access it with class.class.class.variable

1 Like

Ouch, thatā€™s horrible. :smiley:

Just add a getter method for the variable or make it explicitly public.

2 Likes

But I would have to create a getter method for every class it sits inside too, right?

There are probably better ways to debug than hacking private members to be accessible from an outer scope. Like using the debugger or putting jasserts or logging in the inner classes implementationsā€¦

3 Likes

Well, if encapsulation is optional, why not writing C?

I am still undecided, if I call that bad practise or sabotageā€¦

Can I burn this thread for its title?

You really have to ask yourself what in the hell youā€™re trying to do if you need to do something so drastic, and borderline insane!

This looks and smells badā€¦
Not to mention this is very likely compiler and OS dependent behaviourā€¦

Youā€™re a wild-man!

1 Like

Lol. I appreciate the concern everyone but relax, no one is suggesting this go into any release build.

I just wanted to know what it would sound like If I modulated a certain variable and before doing it the ā€œrightā€ way I wanted to test it out quick and dirty.

I have removed the the preprocessor directive already. Balance has been restored to the universe :slight_smile:

1 Like

:joy:

2 Likes

Hijacking the post (but with some correlation to the class calls):

Iā€™ve a FileLoader class that uses itā€™s own thread to load a file and then giving a pointer to the AudioBuffer itā€™s stored. Since Iā€™ll be using that AudioBuffer to be read in an oscillator in a Synth with multiple voices, Iā€™d like to call it only once in the AudioProcessor and pass it to the Oscillator (AudioProcessor->Synth->Voice->Oscillator), otherwise loading the file in the Osc itself (which would avoid a lot of passing mess) is really inefficient since it will create a background thread + load the same file for each voice.
But making setters from the processor to the oscillator seems kinda dirty somehow.

Which would be the neatest way to pass the buffer pointer to the Osc? Or Iā€™m just overcomplicating this and can be made in a simpler way?

Rather than settingā€¦ getting?

So from wherever FileLoader is called, also create a getAudioBufferPointer function, then from the Oscillator class call getAudioBuferPointer()?

While you are at it, this one is also really neat:
#define true (rand() > 0.01)

6 Likes

Handy way to catch use of those pesky ā€œautoā€ variables, at runtime :

#define auto std::cout << "damn auto!\n"; auto
7 Likes

Love these suggestions! How about this?

4 Likes