I have some code below:
class base{
private:
base();
int x;
public:
friend class child;
};
class child{
public:
void display(base* b)
{
b->x = 1;
}
};
int main()
{
base* t;
//t = new base;
child c;
c.display(t);
}
I am not able to assign the object t. I would like to test the value of x.
How can I test this code?
You usually only test the public API of your classes.
So, if it is necessary for you to test the value of
x, most likely that value is relevant for other classes as well. The fact that you declaredchildas friend to accessxsupports that fact.Conclusion:
Make
xpublic or (better) provide getter and possibly setter methods.