静态数据成员/函数 与 非静态数据成员/函数 关系

本文详细介绍了C++中静态成员函数与静态数据成员的相互调用方式,并探讨了非静态成员函数调用静态成员的方法及静态成员函数访问非静态成员的途径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

静态数据成员/静态成员函数

 

静态数据成员/静态成员函数 与静态数据成员/静态成员函数 之间的关系,以及之间如何相互调用。

 

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++教程都有详细的说明。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值