I have a unit test class Tester; I want it to access private fields of a Working class.
class Working {
// ...
private:
int m_variable;
};
class Tester {
void testVariable() {
Working w;
test( w.m_variable );
}
}
I have the following options:
- make m_variable
public- ugly - make method
test_getVariable()- overcomplicated - add
friend class Testerto Working - then Working "knows" about the Tester explicitly, which is not good
My ideal would be
class Working {
// ...
private:
int m_variable;
friend class TestBase;
};
class TestBase {};
class Tester : public TestBase {
void testVariable() {
Working w;
test( w.m_variable );
}
}
where Working knows about TestBase but not each test... but it does not work. Apparently friendship does not work with inheritance.
What would be the most elegant solution here?