Re: Divide Large Class?
On Feb 23, 2:09 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
Immortal Nephi wrote:
[..]
My question is =96 how can one big class break into smaller sub-
classes. [..]
The answer is "inheritance".
struct A { int a; void foo_a(); void bar_a(); };
struct B { double b; void foo_b(); void bar_b(); };
struct C { char c; void foo_c(); void bar_c(); };
struct ABC: A, B, C {};
That can be seen as a better alternative to
struct ABC {
int a;
double b;
char c;
void foo_a();
void bar_a();
void foo_b();
void bar_b();
void foo_c();
void bar_c();
};
Whether it makes sense or not, it's for the programmer to decide. Of
course the problem with inheriting those classes is that 'A::foo_a' has
no way of getting to the 'C' part (so to speak). So, if it needs to,
you would have to keep them together. A possible alternative is to
extract all data into a separate class, a virtual base to all "parts":
struct VeryBase { int a; double b; char c; };
struct A : virtual VeryBase { void foo_a(); void bar_a(); };
struct B : virtual VeryBase { void foo_b(); void bar_b(); };
struct C : virtual VeryBase { void foo_c(); void bar_c(); };
struct ABC: A, B, C {};
V,
I agree with you. Virtual base is needed like I described earlier on
another post before. I refer multiple diamonds meaning you can have
more than 10 sub-classes derived from VeryBase class. Then, all
variables and member functions are inherited to bottom subclass.
Variable as member data is always placed on VeryBase class because
each subclass need to modify data member from VeryBase class.
You can use Run() from ABC subclass as long as you wish. One problem
is that member function can't communicate between A, B, and/or C
subclasses so you have to go through ABC subclass.
For example.
ABC::Run()
{
(this->*pFunc)();
}
A::foo()
{
// do something
ABC::pFunc = &B:hoo; // Error pFunc is not member function to A, B, or
C.
}
ABC::Run()
{
(this->*pFunc)();
}
ABC::pFunc finished running A::foo() and then it assigns a pointer
function to B::Hoo(). Repeat ABC::pFunc inside ABC::Run(). It
repeats and choose one of hundred member function to be reassigned to
pFunc and goes again.
Do you understand what I am trying to explain?