template<class C> class BSTree
{
private:
BSTNode<C>* rootPtr;
staticint treeSize;
protected:
...
public:
//static functions for changing and accessing static variables
staticint getSize(void);
staticvoid setSize(int);
//constructors
BSTree<C>();
//destructors
virtual ~BSTree<C>();
//other functions
int size(void);
};
i want to change the value of the static variable treeSize when the program starts. and then i don't want to touch it except when setSize and getSize (which are static functions) are called from the objects of this.
How do i initialize this variable ?
I tried doing
staticint treeSize = 0;
it doesnt allow that
I could call the function setSize(0) but since it has a template, i have to call it
BSTree<T>::setSize(0);
. now outside the class declaration T is undefined.
Each instance created for a type T will have a separate static int treeSize that is shared among all classes with T = that type, so you must specify the type you wish to modify. When you instantiate a template with T = something, you are basically creating a new class with the type given substituted in for all places where T is used. That's why each different value of T will have it's own static variable, and why you need to specify.
But , that way I impose too much constraint on what types can be formed. even if I specify all basic types, the user just can't define his own type and start working with my library. Is there really no way around? How do STLs solve such a problem (in case they do have such problems)
I can't think of any situation where the STL would need a static variable (though admittedly I haven't put much thought into it), and I can't think of a way around it off-hand.
And the proper way to initialize a static variable would be in the constructor, likely the initializer list. I noticed I didn't answer that part of your question.
Edit: Perhaps you could derive from a non-template class that has the static variable in it? Though that does sound a bit sloppy.
ya i too thought of initializing in a constructor but its kinda bad becuse everytime i create a node this variable is switched to a 0. if i have 1000 nodes, i have 1000 extra lines to execute.
but if there is no way around then this is the best option i guess. and to update the variable before using.
and deriving from non-template class sounds really good coz then i still maintain the freedom to use any data-type in my library... i'll definitely look into that..
Why a static variable? Isn't treeSize a count of the number of nodes in a given tree?
A tree is a single instance of BSTree. Just make treeSize a regular int member of BSTree.
Assign treeSize = 0 in the BSTree constructor (when an instance of a tree is created), then increment it in your addNode() (or insert() or push(), whatever you call it). This way you keep a count of the number of nodes in each tree.
And the proper way to initialize a static variable would be in the constructor, likely the initializer list. I noticed I didn't answer that part of your question.
Huh? I'll just assume that you slipped up on that one - since when do you initialize static variables in the constructor?
also,
i know i have to initialize this static variable for the first time in a .cpp file. but if i want all my implementation to only constitute 1 or more header files. then how do i initialize this static variable.
coz doing BSTree::treeSize = 0; in a header file is giving me errors. there must be compiler and linker issues with this definitely.
No, it doesn't have to be in the .cpp, but that approach would probably be preferred - I don't really know what would happen if you tried to initialize it twice (which is what would happen if you put it in the header file, and use that header file in another project and link the object file of your cpp).