changing static variable in a class

Mar 10, 2011 at 8:16pm
Hi All,

I have a class :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
template<class C> class BSTree
{
    private:
        BSTNode<C>* rootPtr;
        static int treeSize;
    protected:
        ...
    public:
        //static functions for changing and accessing static variables
        static int getSize(void);
        static void 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
 
static int 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.


How do I solve this problem ?

~cheers!
navderm
Last edited on Mar 10, 2011 at 8:17pm
Mar 10, 2011 at 8:43pm
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.
Mar 10, 2011 at 8:46pm
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)

Mar 10, 2011 at 8:50pm
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.
Last edited on Mar 10, 2011 at 8:52pm
Mar 10, 2011 at 8:56pm
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..

thx a lot !
~Cheers!
navderm
Mar 10, 2011 at 10:11pm
closed account (D80DSL3A)
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.
Mar 11, 2011 at 5:38pm
@fun2code:

ya thats definitely a much smarter way. !!
thx
Mar 11, 2011 at 6:02pm


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?
Mar 11, 2011 at 6:15pm
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.
Mar 11, 2011 at 6:22pm
also
when i define another class
1
2
3
4
5
6
7
8
9
class staticVars
{
    private:
        static unsigned int theSize;
    public:
        //static functions for changing and accessing static variables
        static int getSize(void);
        static void setSize(int);
};


and i try to define the functions
1
2
3
4
5
6
7
8
9
int staticVars::getSize(void)
{
    return (staticVars::theSize);
}

void staticVars::setSize(int newSizeVal)
{
    staticVars::theSize = newSizeVal;
}


i get the error
undefined reference to staticVars::theSize
How is that even possible !!!!
Mar 11, 2011 at 6:27pm
Initialization of static members is done like this:
1
2
3
4
5
6
7
class useless_class
{
private:
static int n;
};

int useless_class::n = 0;


Why your linker is giving you errors there I can only guess, that depends on the rest of your code.
Mar 11, 2011 at 6:30pm
does this initialization have to be in a .cpp. ? i am doing in .h
if yes, why ??
Last edited on Mar 11, 2011 at 6:31pm
Mar 11, 2011 at 6:37pm
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).
Mar 11, 2011 at 8:42pm
hanst99 wrote:
Huh? I'll just assume that you slipped up on that one - since when do you initialize static variables in the constructor?


I have no idea what I was thinking when I wrote that, as it is very wrong.
Mar 11, 2011 at 9:27pm
No problem, everyone has this kind of "What was I thinking" moments once in a while ;)
Topic archived. No new replies allowed.