I have made a console application which does something. It looks like:
class C
{
public:
void print()
{
print("something that only class C knows");
}
}
//**************************************
class B
{
public:
void print()
{
print("something that only class B knows");
for_each(C c)
{
c.print();
}
}
private:
std::vector<C> c_classes;
}
//**************************************
class A
{
public:
void print()
{
print("something that only class A knows");
for_each(B b)
{
b.print();
}
}
private:
std::vector<B> b_classes;
}
And works nice in console, where I can print from everywhere.
The problem appears when I want to port this application to GUI. I need to show everything that classes A, B and C knows. What is the best way to do this?
I thought to make class A return reference to vector of classes B (which returns reference to vector of classes C) and get values from each instance in GUI class.
But I don’t want to make GUI class so much dependent from these classes.
Is there any better way to do this?
Make a common interface
Printablethat has a pure virtualprint()method (possibly returning a string), and have a vector of pointers to this objects somewhere. Have classesA,BandCderive fromPrintable. So when you want to print the stats of each object, just iterate through this vector and display the output.