Thread: static bool question...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    77

    static bool question...

    I can't find a definitive answer on the proper use of "static" for declaring a variable.

    I have a bool called get_data. I use it in a procedure that is declared as a "static void function_name". I tried to declare get_data as a regular bool, however I recieved a compiler error "illegal reference to a data member in a static function". I then declared get_data as static bool to fix the compiler error.

    I need help in understanding what the static does.

    Another question is: I set get_data in the static function, then I pass it through an ethernet packet to another computer. Will get_data retain it's value when it gets there?

    EDIT:
    When I link I get the following error:
    unresolved external symbol "public: static bool get_data"


    Thanks in advance!
    JK
    Last edited by jerrykelleyjr; 02-17-2005 at 09:06 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    It sounds like you are describing a class that has a static function which tries to modify/set one of the classes data members. Is this right?

    A static class function can only work on static class data member's (or local function variables since they are created/destroyed within the scope of that function). The reason for this is that a static member function can be called without a particular instance of said class being in existance at the time it is called. So, if no instances of the class existed, how would the static function be able to set a data member that doesn't exist yet? Since static data members also exist outside of any particular instance of a class, they are the only data members that can be accessed within a static function.

    This kind of setup allows a variable to be set before instantiation of a class. The constructor can then take special steps depending on the values of these static data members.

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    class MyClass
    {
        static bool m_bValue;
    public:
        MyClass();
        ~MyClass();
        static void SetBool(bool);
    };
    
    MyClass::MyClass()
    {
        cout << "Constructor called, m_bValue is: "
             << boolalpha << m_bValue << endl;
    }
    
    MyClass::~MyClass()
    {
        cout << "Destructor called." << endl;
    }
    
    void MyClass::SetBool(bool val)
    {
        m_bValue = val;
        cout << "Setting m_bValue to: "
             << boolalpha << m_bValue << endl;
    }
    
    bool MyClass::m_bValue = false;
    
    int main()
    {
        // Set value of static bool to true, notice no instance of MyClass
        // exists at this point in time
        MyClass::SetBool(true);
    
        // Create instance of class, then destroy it
        MyClass* ptr = new MyClass;
        delete ptr;
    
        // Now set static bool to false.  Again, no instance of MyClass
        // exists at this point in time
        MyClass::SetBool(false);
    
        // Create new instance, then destroy it
        ptr = new MyClass;
        delete ptr;
    
    }
    Output should be:
    Code:
    Setting m_bValue to: true
    Constructor called, m_bValue is: true
    Destructor called.
    Setting m_bValue to: false
    Constructor called, m_bValue is: false
    Destructor called.
    Quote Originally Posted by jerrykellyjr
    EDIT:
    When I link I get the following error:
    unresolved external symbol "public: static bool get_data"
    I think I know why you get this. See the code in my example above in blue. Since static data members exist outside of any instance of a class, you need to create instances of them separately outside of the class in the manner indicated.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    thanks hk!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. question about bool
    By ssharish2005 in forum C Programming
    Replies: 7
    Last Post: 10-17-2005, 08:18 AM
  3. Question about Static variables and functions.
    By RealityFusion in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2005, 02:31 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. static int class question...
    By ss3x in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2002, 09:00 PM