Reference Counting

What is meant by Reference Counting?
Quick FYI: std::shared_ptr is an implementation of reference counting.
I have read that article on wikipedia about reference counting but I did not understand much, partly because there were no examples included. Can you please elaborate in simple words with an example?
Make an object. Keep track of everything that wants to use it. When everything that wants to use it no longer wants it, destroy that object.

The act of keeping count of how many things want to use the object is called reference counting.
I think something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class myClass
{
   private:
      static int refCount;
   public:
      myClass():refCount(0)
      {
         refCount++;
      }
      ~myClass()
      {
         refCount--;
      }
};
Topic archived. No new replies allowed.