Hello there,

I have a design issue that I'd like to share with you and hopefully get some suggestions to solve what should be a straight forward problem.

I have a class (classA) that uses reference counting on the private data members - much like Qt does. This class is one of many classes that share a number of features, like logging and error checking - which are are done on the private data members (eg "p->_logMessage", where "p" is a pointer to the shared reference counted data).

So I've decided to put some common functionality into a separate class, classB, which classA now inherits. What I'd like to do is to access various private methods of classA from classB.

So,

class classB {
public:
classB() {}
void someFunc() { /* do stuff and log any errors*/ }
};

struct classAPrivate {
string error;
etc etc
};

class classA : public classB {
public:
classA() : classB() {}
setError(string msg) { p->error = msg; }
private:
classAPrivate* p;
}

So in classB I'd like to somehow be able to access the "setError" function for classA. Eg when I call someFunc(), the error message may need to be set in classA.


One solution I've got is to declare another abstract class, classC, which declares the setError() method as pure virtual. A pointer to classC is then given to classB in the constructor and stored as a private data member. Now it is possible to call setError() from classB using the classC pointer - which will call the implementation defined in classA. Phew!

Question is, is there a better way of doing this? I've considered using functors for this, but this wouldn't be any cleaner than my current implementation - in fact a whole lot messier. Perhaps the command pattern?

The problem could be condensed into the following:

"How can I call private methods in a derived class from a base class".

Any help much appreciated!