I need to set callback, but I dont want to make it global function, I dont need it public aswell, so I’ve made it private. Wondering, is it the right way to go.
class A // button object from window library. I wont change this class
{
public:
typedef void (*fptr)();
void set(fptr p)
{
p(); // here I call private static of B
};
};
class B // my own class
{
private:
static void prfn() {};
public:
B()
{
A a;
a.set(prfn);
};
};
int main(){B b;}
Your code is fine. It is no different from the following standard idiom:
It’s perfectly fine to expose private members via public functions. (It may not be good design, but it’s entirely legal.) You’re doing the same thing by using a private member as a function argument for some other, unrelated purpose. Note that it is only your own
class Bthat accesses a private member ofB.