静态数据成员/静态成员函数
静态数据成员/静态成员函数 与静态数据成员/静态成员函数 之间的关系,以及之间如何相互调用。
1. 静态成员函数 可以直接访问 静态数据成员/静态成员函数
如例程所示:
#include <iostream>
#include <string>
using namespace std;
class Student
{
static int number; // 静态数据成员
string name;
public:
void set(string str)
{
name = str;
number++;
}
static void print() // 静态成员函数print()
{
cout <<"1: The number of the students is " << number << " numbers." << endl; // 调用静态数据成员
print2(); // 调用静态成员函数
}
static void print2() // 静态成员函数print2()
{
cout <<"2: The number of the students is " << number << " numbers." << endl;
}
};
int Student::number = 0; // 静态数据成员初始化
int main(int argc, char** argv)
{
Student s;
s.set("smith");
s.print();
return 0;
}
结果如下:
1: The number of the students is 1 numbers.
2: The number of the students is 1 numbers.
2. 非静态成员函数 调用 静态数据成员/静态成员函数
例程如下:
#include <iostream>
#include <string>
using namespace std;
class Student
{
static int number; // 静态数据成员
string name;
public:
void set(string str)
{
name = str;
number++; // 调用静态数据成员
print(); // 调用静态成员函数print()
print2(*this); // 调用静态成员函数print2()
Student::print3(*this); // 调用静态成员函数print3()
}
static void print() // 静态成员函数print()
{
cout <<"1: The number of the students is " << number << " numbers." << endl; // 调用静态数据成员
}
static void print2(Student& s) // 静态成员函数print2()
{
cout <<"2: The number of the students is " << number << " numbers." << endl;
}
static void print3(Student& s) // 静态成员函数print3()
{
cout <<"3: The number of the students is " << number << " numbers." << endl;
}
};
int Student::number = 0; // 静态数据成员初始化
int main(int argc, char** argv)
{
Student s;
s.set("smith");
//s.print();
return 0;
}
运行结果如下:
1: The number of the students is 1 numbers.
2: The number of the students is 1 numbers.
3: The number of the students is 1 numbers.
从例程可得知:非静态成员函数可以直接访问静态数据成员和静态成员变量,但是当静态成员函数调用了类的对象Student& s,则需要使用this指针,即
print2(*this); // 调用静态成员函数print2()
Student::print3(*this); // 调用静态成员函数print3()
3. 静态成员函数 通过 类对象 访问 非静态数据成员/非静态成员函数
例程如下:
#include <iostream>
#include <string>
using namespace std;
class Student
{
static int number; // 静态数据成员
string name;
public:
void set(string str)
{
name = str;
number++; // 调用静态数据成员
}
void print() // 态成员函数print()
{
cout <<"1: The number of the students is " << number << " numbers." << endl; // 调用静态数据成员
}
static void print2(Student& s) // 静态成员函数print2()
{
cout <<"2: The number of the students is " << number << " numbers." << endl;
cout << s.name << endl; // 调用非静态数据成员
s.print(); // 调用非静态成员函数
}
};
int Student::number = 0; // 静态数据成员初始化
int main(int argc, char** argv)
{
Student s;
s.set("smith");
s.print2(s);
return 0;
}
运行结果如下:
2: The number of the students is 1 numbers.
smith
1: The number of the students is 1 numbers.
从例程可以得知,静态成员函数中调用非静态数据成员和非静态成员函数,则需要通过类对象来实现。
4. 静态成员函数中不能使用this指针
例程如下:
#include <iostream>
#include <string>
using namespace std;
class Student
{
static int number; // 静态数据成员
string name;
public:
void set(string str)
{
name = str;
number++; // 调用静态数据成员
}
void print() // 态成员函数print()
{
cout <<"1: The number of the students is " << number << " numbers." << endl; // 调用静态数据成员
}
static void print2(Student& s) // 静态成员函数print2()
{
cout <<"2: The number of the students is " << s.number << " numbers." << endl;
string strName = this->name; // 使用this 指针
}
};
int Student::number = 0; // 静态数据成员初始化
int main(int argc, char** argv)
{
Student s;
s.set("smith");
s.print2(s);
return 0;
}
编译程序,得到如下错误:
error C2671: 'Student::print2' : static member functions do not have 'this' pointers
error C2227: left of '->name' must point to class/struct/union/generic type
静态成员函数是没有this指针的,所以也没得用。
以上都是静态成员函数/静态数据成员 与 静态成员函数/静态数据成员 之间的相互调用,以及常见的情况的简单罗列,对于其具体的原理,网上的文章以及任何一本c++教程都有详细的说明。