| | |
Practical application of static member function
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
kimfalk posed a question about static functions in a class in http://www.daniweb.com/techtalkforums/thread19241.html to which my reponse was duly correct by Of 262 pages of text devoted to the subject of classes in C++ Primer Plus, I somehow missed the two paragraphs that were devoted to this topic.
I'm assuming the lack of attention is due to unlikelyhood of use, but would appreciate a practical example that is not taken from any theoretical text. To me it seems somewhat analogous to declaring all variables public, defeating the purpose of encapsulation to begin with and OOP as a whole.
•
•
•
•
Originally Posted by Narue
You're mixing up your statics.static is overloaded for too many different uses. In a class declaration, static means that the name belongs to the class and not to individual objects. In a function definition, static means that the variable being declared has static storage duration. In the global scope, static is a deprecated feature that forces internal linkage.
I'm assuming the lack of attention is due to unlikelyhood of use, but would appreciate a practical example that is not taken from any theoretical text. To me it seems somewhat analogous to declaring all variables public, defeating the purpose of encapsulation to begin with and OOP as a whole.
>would appreciate a practical example that is not taken from any theoretical text
It's often useful to keep a reference count of how many objects are in existence at any given time. However, it's not always practical to have an object to call a member function on. Therefore, you need a static class variable to keep the reference count and a static class function to get the reference count without having to access an object:
It's often useful to keep a reference count of how many objects are in existence at any given time. However, it's not always practical to have an object to call a member function on. Therefore, you need a static class variable to keep the reference count and a static class function to get the reference count without having to access an object:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; class A { public: A() { ++ref_count; } public: static int references() { return ref_count; } private: static int ref_count; }; int A::ref_count = 0; void stuff() { cout<< A::references() <<endl; } int main() { A a, b, c; stuff(); }
New members chased away this month: 5
![]() |
Similar Threads
- hi I need some help in calling c++ non static member function in c call back function (C++)
- Create static variable in non static member function (C++)
- errorcode C2352 - illegal call of a non static member function. (C++)
- performance benefit by not calling static member function by object (C)
- Change form's cursor in a static member function (C)
Other Threads in the C++ Forum
- Previous Thread: Error message when compiling in MS Vis
- Next Thread: I need help with C++
Views: 4121 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets




static is overloaded for too many different uses. In a class declaration, static means that the name belongs to the class and not to individual objects. In a function definition, static means that the variable being declared has static storage duration. In the global scope, static is a deprecated feature that forces internal linkage.

