I suggest you post the code. I have summarized some knowledge points about static data members and static member functions. I hope it will be useful to you!
Static member functions are not associated with any object, so
1. Non-static data members belonging to class objects cannot be accessed
class A
{
private:
int a;//non-static
public:
A(int init):a(init){}
static getvalue(){return a;}/*错误,无法访问属于类对象的非静态数据成员*/
};
2. Unable to access non-static member functions
class A
{
private:
int a;//non-static
public:
A(int init):a(init){}
int getvalue(){return a;}//non-static
static printvalue(){cout << getvalue() << endl;}/*错误,无法访问非静态成员函数*/
};
3. Only other static member functions can be called
class A
{
private:
static int staticvalue;
int a;//non-static
public:
A(int init):a(init){}
static int getvalue(){return staticvalue;}
static printvalue(){cout << getvalue() << endl;}
};
int A::staticvalue = 2;//类外的定义
int main(int argc , char *argv[])
{
A demo(1);
demo.printvalue();
A::printvalue();
return 0;
}
BTW: Static data members must be defined outside the class
There is a problem with what was said above. It is best for static functions to only reference static data members, but they can also reference non-static data members. If you refer to non-static data members in the object, you can declare the function static bool foo(A & a). lzThe const std::string& iVariable here is not a data member in class A. Why should we use a static function to handle it?
I suggest you post the code. I have summarized some knowledge points about static data members and static member functions. I hope it will be useful to you!
Static member functions are not associated with any object, so
1. Non-static data members belonging to class objects cannot be accessed
2. Unable to access non-static member functions
3. Only other static member functions can be called
BTW: Static data members must be defined outside the class
There is a problem with what was said above. It is best for static functions to only reference static data members, but they can also reference non-static data members. If you refer to non-static data members in the object, you can declare the function static bool foo(A & a).
lz
The const std::string& iVariable here is not a data member in class A. Why should we use a static function to handle it?