Use of 'static' in a class

This is a discussion on Use of 'static' in a class within the C++ Programming forums, part of the General Programming Boards category; What does it mean when a member variable or a member function of a class is static? Code: // .h ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    Use of 'static' in a class

    What does it mean when a member variable or a member function of a class is static?

    Code:
    // .h file
    class example
    {
    public:
    	static int doSomething();	// What does that static mean here?
    
    private:
    	static int myVariable;		// What does that static mean here?
    }
    
    
    // .cpp file
    
    // If I dont do this, I get an compiler error. Why do I need to do it?
    int example::myVariable;
    
    int example::doSomething()
    {
    	// Can only acces static member variables. Why?
    
    	return 2;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    It means that the member function can be called without an object of the class, and that the member variable can be accessed without an object of the class.

    EDIT:
    Perhaps Stroustrup's glossary would be more accurate:
    static member - member of a class for which there is only one copy for the whole program rather than one per object.

    static member function - a member function that need not be called for an object of the class.
    Last edited by laserlight; 01-21-2009 at 09:52 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    I am working on binding my C++ code to a lua script, and to bind a function the function needs to be "static int something(lua state)"

    From what I understand a static is almost like a global, which makes me believe that the following code is not thread safe (I would like it to be thread safe)

    Code:
    // the main program: 
    example a;
    a.load();
    while(for some time)
    	a.run();
    
    
    // example.h file
    class example
    {
    public:
    	bool load();
    	static int functionToBind();
    	void run() const;
    
    private:
    	Lua scriptState;
    	static VariableCollection myVar;
    } 
    
    
    // example.cpp file
    
    VariableCollection example::myVar;
    
    bool example::load()
    {
    	// Load and init the lua script state
    
    	// Bind the functionToBind so that it can be called from lua
    
    	return true;
    }
    
    
    static int example::functionToBind()
    {
    	// use some of the variables in myVar to do some calculations
    	// The myVar are different for each instance of the example class
    
    	// Get some info from the script
    
    	// Pass some info that is calculated from myVar to the script
    }
    
    void example::run() const
    {
    	// run a lue script that can call functionToBind
    }

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Indeed it isn't. If you want per-thread state, you'll have to bind it into the Lua context somehow.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  3. Static member of a class
    By Gravedigga in forum C++ Programming
    Replies: 2
    Last Post: 08-15-2005, 03:54 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Replies: 3
    Last Post: 10-10-2002, 07:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21