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.
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.