Different versions of a class where static member changes

N

nw

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.
 
A

Andrey Tarasevich

nw said:
...
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top