K
Kasper Middelboe Petersen
Lets say I have these 3 classes:
Class Parent {};
Class Child : public Parent {};
Class Priest {};
Since it is a library I'm creating the following two problems arise:
1. I have no control over what the name of "Priest" as this is up to
the person using the library in the first place to create - and he
needs more than one different class extending Parent at the same time.
2. I need Child to pass one or more callback functions on to Priest
which allows it to access Child's private parts!
This is what I've come up with so far and it seems to work:
class Child : public Parent {
public:
Child() : priest(callbackfunc, this) {};
static void callbackfunc(Parent* p) {
std::cout << "Mmm... Private " << (Child*)p->parts << std::endl;
};
void run() {
priest.grab();
};
private:
Priest priest;
int parts;
};
class Priest {
public:
Priest(void (*_callbackfunc)(Parent*), Parent *_p)
: callbackfunc(_callbackfunc), p(_p) {};
void grab() {
(*callbackfunc)(p);
};
private:
void (*callbackfunc)(Parent*);
Parent* p;
};
But I'm not sure if this is the correct way to solve this or if there
is a better/cleaner one?
Thanks,
Kasper
Class Parent {};
Class Child : public Parent {};
Class Priest {};
Since it is a library I'm creating the following two problems arise:
1. I have no control over what the name of "Priest" as this is up to
the person using the library in the first place to create - and he
needs more than one different class extending Parent at the same time.
2. I need Child to pass one or more callback functions on to Priest
which allows it to access Child's private parts!
This is what I've come up with so far and it seems to work:
class Child : public Parent {
public:
Child() : priest(callbackfunc, this) {};
static void callbackfunc(Parent* p) {
std::cout << "Mmm... Private " << (Child*)p->parts << std::endl;
};
void run() {
priest.grab();
};
private:
Priest priest;
int parts;
};
class Priest {
public:
Priest(void (*_callbackfunc)(Parent*), Parent *_p)
: callbackfunc(_callbackfunc), p(_p) {};
void grab() {
(*callbackfunc)(p);
};
private:
void (*callbackfunc)(Parent*);
Parent* p;
};
But I'm not sure if this is the correct way to solve this or if there
is a better/cleaner one?
Thanks,
Kasper