Re: templates & classes
Pete Becker wrote:
Fei Liu wrote:
Pete Becker wrote:
Fei Liu wrote:
The description of the OP's problem requires that 2 classes do not
share the storage of those static members. In this case, derivation
won't work.
Of course it will. The things that aren't shared don't go in the base
class.
It will what?
"In this case, derivation won't work."
"Of course it will.
What, specifically, do you claim "won't work"? Whatever is defined in
the base class is part of the base class, and whatever is defined in the
derived classes is part of the derived classes. So if you want the
derived classes to hold something in common, put it in the base class.
If you want the derived classes to hold independent things, put them in
the derived classes. The result is that the derived classes both have
whatever is in the base class, and each has whatever is defined for it.
Clearly you don't understand the OP's spec, hint it's a device driver.
Your logical representation fails to meet the requirement of the
physical model.
struct base
{
static int i;
static void f();
};
struct derived1: base
{
static int j;
static void g();
};
struct derived2: base
{
static int k;
static void h();
};
Now, base has two members, i and f. derived1 has four members. It
inherits i and f from base, and it has the two in its definition, j and
g. derived2 has four members. It also inherits i and f from base, and it
has two in its definition, k and h.
So, once again: the members that are the same for both derived types
should be defined in base, and the members that are different should be
defined in their respective derived classes.
The storage of derived classes is shared in base. The CRTP solution
posted in this thread is very nice though.
Using templates where inheritance is more appropriate isn't "nice."
Again you demonstrate that you do not understand the OP's problem.