Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Different versions of a class where static member changes (http://www.velocityreviews.com/forums/t646295-different-versions-of-a-class-where-static-member-changes.html)

nw 11-27-2008 11:06 AM

Different versions of a class where static member changes
 
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.

Andrey Tarasevich 11-27-2008 11:26 PM

Re: Different versions of a class where static member changes
 
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


All times are GMT. The time now is 04:33 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.