[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Different versions of a class where static member changes

nw

11/27/2008 11:06:00 AM

Hi,

I'd like to have a set of classes which differ in that a static member
has a different value (obviously they share other functionality not
shown here). My currently proposed solution is this:

template<int _probability_id>
class MyClass {
public:

MyClass() {
}

static MyStaticMemberClass param;

...

};

Where I'd create different versions of the class all of which share
the static member with:

MyClass<0> type0Object; //shares static member with
object below
MyClass<0> anotherType0Object;

MyClass<1> type1Object; //different static member than
above, same as below.
MyClass<1> anotherType1Object;

I've not seen a solution like this before, and am wondering if it's a
valid and sensible thing to do? Is there a better solution that
someone can suggest?

One suggestion might be not to use a static member, however for this
application the overhead of the object or even a reference would be
significant (the objects are small and there are lots of them). In my
case I think the static member models the problem correctly as well
(all objects of a given type should have the same params).

Thanks for reading.
1 Answer

Andrey Tarasevich

11/27/2008 11:27:00 PM

0

nw wrote:
> ...
> I've not seen a solution like this before, and am wondering if it's a
> valid and sensible thing to do?
> ...

What you did in this case is exactly what you need, if I understand the
description of the problem correctly. So, if you are OK with such things
as different versions of the class being completely unrelated types (as
'MyClass<0>' and 'MyClass<1>'), then yes, it is a valid and perfectly
sensible thing to do.

--
Best regards,
Andrey Tarasevich