Different versions of a class where static member changes

Discussion in 'C++' started by nw, Nov 27, 2008.

  1. nw

    nw Guest

    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.
    nw, Nov 27, 2008
    #1
    1. Advertising

  2. 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
    Andrey Tarasevich, Nov 27, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,755
    Thomas Weidenfeller
    Oct 12, 2005
  2. Siemel Naran
    Replies:
    4
    Views:
    805
    Micah Cowan
    Jan 12, 2005
  3. dolphin
    Replies:
    3
    Views:
    1,343
    Pete Becker
    Dec 5, 2007
  4. ks
    Replies:
    9
    Views:
    413
    Jorgen Grahn
    Mar 20, 2010
  5. Ken Bloom
    Replies:
    2
    Views:
    125
    Ken Bloom
    Nov 4, 2008
Loading...

Share This Page