944,131 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5449
  • C++ RSS
Feb 26th, 2005
0

Practical application of static member function

Expand Post »
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
Quote 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.
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.
Reputation Points: 47
Solved Threads: 17
Posting Whiz in Training
Tight_Coder_Ex is offline Offline
215 posts
since Feb 2005
Feb 26th, 2005
0

Re: Practical application of static member function

>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:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class A {
  6. public:
  7. A() { ++ref_count; }
  8. public:
  9. static int references() { return ref_count; }
  10. private:
  11. static int ref_count;
  12. };
  13.  
  14. int A::ref_count = 0;
  15.  
  16. void stuff()
  17. {
  18. cout<< A::references() <<endl;
  19. }
  20.  
  21. int main()
  22. {
  23. A a, b, c;
  24.  
  25. stuff();
  26. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Feb 26th, 2005
0

Re: Practical application of static member function

Got it. I'm going to experiment what will happen by calling without scoping and referencing ref_count as a.ref_count etc.

I still don't see a practical way I can use this, but your example makes it crystal so if I elect to do so I shouldn't have any problems.
Reputation Points: 47
Solved Threads: 17
Posting Whiz in Training
Tight_Coder_Ex is offline Offline
215 posts
since Feb 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Error message when compiling in MS Vis
Next Thread in C++ Forum Timeline: I need help with C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC