What is the best approach to this data structure?
I have two classes that are almost the same, inn classA I have a protected/private struct that differs slightly from the struct in classB.
What I need is something like a BaseClass which hold the generalization, the basic struct and some common functions, and two derived classes which extend the BaseClass and its struct and functions.
Furthermore the DerivedClassA has a pointer var to DerivedClassB...
Yes, this seems messy… I know! This is a school assignment…
I’m not too sure about the extending of the struct stuff… Union? :eek:
You should not need a union to do this. You need to inherit the BaseClass and then add the extra elements in the derived classes.
Txn for replay!
What confuses me though is the struct, the assignment implies that there is a method for extending the struct inside the base class… I might be on completely of course here..
Well, you shouldn't be allowed to access a struct inside the private area, so no external code would know what the struct looks like.
Particularly when it comes to derived classes, there should never be direct access to the data inside the class, as that is bound to fail when you introduce a new derived class. [Of course, you also can't use any of the extra data without checking if the class supports that - you can either write virtual functions that return "nothing" when you ask for the extra data that isn't there, or some sort of "isa" function to check if the derived class supports some certain functionality].