Practical application of static member function

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Practical application of static member function

 
0
  #1
Feb 26th, 2005
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
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,867
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Practical application of static member function

 
0
  #2
Feb 26th, 2005
>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:
  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. }
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: Practical application of static member function

 
0
  #3
Feb 26th, 2005
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 4121 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC