Sign in to follow this  
Followers 0
Vincent_M

Inheriting Static Data Members

4 posts in this topic

I have the following classes in C++:

class CollectionItem {...};

class Collection {...};

 

Collection managers a list of CollectionItem instances. CollectionItem has a char[32] "name" property and a type[32] "type". The name property is meant to be unique for that specific CollectionItem instance while "type" is for grouping, and Collection is only meant to add CollectionItem instances who's "type" meets Collection's "key" char[32].

 

CollectionItem is meant to be an abstract class to be instanced like so:

class Object : public CollectionItem {}; // Object's inherited "type" will be "scene_object"

class PointLight : public CollectionItem {}; // PointLight's inherited "type" will be "scene_pointlight"

class Model : public CollectionItem {}; // Model's inherited "type" will be "scene_model"

 

As you can see all "Object" instances are meant to have the same value for "type", while PointLight instances will have their own group value, etc. The problem is that I'm using a bunch of redundant memory. Each instance of CollectionItem results in an extra 32 bytes of the same string... Thousands of these items can exist at once in some applications. I know that's only 32KB of fluff space per 1024 CollectionItems, but I'd like to cut that down if possible.

 

I'd like to make "type" static, and change its value for each class it inherits from. Problem is, inheriting classes don't get their own class-wide copy of that static data member. By design, inherited classes all share their superclass counterpart's static member.

 

I could just declare a static member of char[32] "type" in all subclasses, but Collection isn't going to evaluate subclass versions of "type", it's only going to see CollectionItem's type.

 

Any ideas?

0

Share this post


Link to post
Share on other sites

I'm not sure i understand.  When the object is built only one copy of type is included, and set to the appropriate value.  Each object in your design needs to have this to be recognized.  How is this redundant?

 

If you're concerned about redunant menory, why not just make multiple instances of the collections object and use them to roganize specific entites?

0

Share this post


Link to post
Share on other sites

If you were to change "type" to a pointer, you could take advantage of a compiler optimization called "string pooling."  So, regardless of how many CollectionItem instances you have, there will only be one copy of the string in memory for each derived type.

Edited by shuma-gorath
0

Share this post


Link to post
Share on other sites

Actually, I think Paradigm Shifter has the solution. Sounds so simple, I can't believe I didn't even consider that. Perfect!

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0