大佬教程收集整理的这篇文章主要介绍了C++静态成员函数(无师自通),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
static <return type><function name> (<parameter list>)
静态成员函数通常用于处理类的静态成员变量。实际上,对于类的成员函数来说,如果它不能访问类的任何非静态成员,则应该将该函数变成静态成员函数。//budget2.h的内容 #ifndef BUDGET_H #define BUDGET_H class Budget { private: static double corpBudget; double divBudget; public: Budget() { divBudget =0; } void addBudget(double b) { divBudget += b; corpBudget += divBudget; } double getDivBudget() const { return divBudget; } static double getCorpBudget() { return corpBudget; } static void mainOffice(doublE); }; #endif //budge t2.cpp 的内容 #include "budget2.h" // DeFinition of the static member of Budget class. double Budget::corpBudget = 0; void Budget::mainOffice(double budReq) { corpBudget += budReq; } //main程序的内容 // This program demonstrates a static class member function. #include <iostream> #include <iomanip> #include "budget2.h" // For Budget class declaration using namespace std; int main() { const int n_DIVISIONS = 4; // Get the budget requests for each division cout << "Enter the main office's budget request: "; double amount; cin >> amount; // Call the static member function of the Budget class Budget::mainOffice(amount); // Create instances of the Budget class Budget divisions[N_DIVISIONS]; for (int count = 0; count < N_DIVISIONS; count++) { double bud; cout << "Enter the budget request for division "; cout << (count + 1) << ": "; cin >> bud; divisions[count].addBudget(bud); } // Display the budget for each division cout << setprecision (2); cout << showpoint << fixed; cout << "\nHere are the division budget requests:\n"; for (int count = 0; count < N_DIVISIONS; count++) { cout << "\tDivision " << (count + 1) << "\t$ "; cout << divisions[count].getDivBudget() << endl; } //Print @R_711_10586@l budget requests cout << "@R_711_10586@l requests (including main officE) : $ "; cout << Budget::getCorpBudget() << endl; return 0; }程序输出结果:
Enter the main office's budget request: 400000
Enter the budget request for division 1: 102000
Enter the budget request for division 2: 210000
Enter the budget request for division 3: 240000
Enter the budget request for division 4: 105000
Here are the divisionbudget requests:
Division 1 $ 102000.00
Division 2 $ 210000.00
Division 3 $ 240000.00
Division 4 $ 105000.00
@R_711_10586@l requests (including main officE): $ 1057000.00
cout << divisions[0].getCorpBudget() << endl;
this 指针不能在静态成员函数中使用,因为静态成员函数不是通过它们所属类的任何实例调用的。而且,静态成员函数除非指定该成员属于哪个实例,否则不能访问其类的实例成员。class StatAccess { private: int x; public: static void output() { cout << x; //对非静态成员的不正确访问 } StatAccess(int X) { this->x = x; } };在语句
cout<<x
中试图访问 x 是不正确的,因为它等于隐式使用 this 指针,而这是静态函数 output 所不具有的。相反,在以下修改过的同一个类的示例中,静态成员函数 print 正确地访问了非静态成员 x,因为它使用了传递给它的类对象的名称作为形参来限定它。class StatAccess { private: int x; public: static void print(StatAccess a) { cout << a.x; } StatAccess(int X) { this->x = x; } };静态成员函数的一个优点是可以在任何类的实例被创建之前调用它们,这使得它们可以用来执行复杂的初始化任务,这些任务必须在创建类的对象之前完成。
以上是大佬教程为你收集整理的C++静态成员函数(无师自通)全部内容,希望文章能够帮你解决C++静态成员函数(无师自通)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。