Can anybody tell me how to implement reference counting with C++?

This is a discussion on Can anybody tell me how to implement reference counting with C++? within the C++ Programming forums, part of the General Programming Boards category; Could somebody recommend any reference/webpages to read? Thank you! BTW, what's the relationship between reference counting and auto pointer?...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Question Can anybody tell me how to implement reference counting with C++?

    Could somebody recommend any reference/webpages to read? Thank you!

    BTW, what's the relationship between reference counting and auto pointer?

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Any suggestions? Thanks!

  3. #3
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    i didn't quite get your question. Do you wanna have a variable that counts, for example, how many times the function have been called??
    yo could do it using static variable
    Code:
    void function()
    {
     static counter = 0;
     counter += 1;
    
     cout << "The function has been called for the " << counter;
     if(counter == 1)
     cout << " time" << endl;
    
     else if(counter > 1)
     cout << " times" << endl;
    }
    
    int main()
    {
            function();
    }
    Last edited by Hussain Hani; 06-03-2007 at 09:16 PM.
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  4. #4
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Reference counting is used quite a bit in Objective C, and I believe you could implement it with something like this:

    Code:
    class Thing
    {
    private:
        int RefCount;
    
        Thing() : RefCount(1) { }
    
    public:
        static Thing *MakeThing() { return new Thing; }
    
        void Retain() { RefCount++; }
        void Release() { if (--RefCount == 0) delete this; }
        //Whatever else the object does.
    };
    A class like this can only be instantiated with the MakeThing function, since the constructor is private. By using the Retain and Release methods, you can increment and decrement the reference count. Once your last pointer releases the object, it will automatically delete itself.

    This is pretty much how the NeXTStep/OpenSTEP/Cocoa Objective C frameworks do it. There may be a better C++ method, though

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    Start here (and google afterwards): http://www.codeproject.com/useritems/SmartPointers.asp

    Note that you didn't mention for what exactly. So its possible that you may not even need reference counting. Smart pointers can be implemented through a few other techniques (of which reference counting is one) depending on what exactly you plan to do with the allocated memory.

    So, for a better picture of the whole of the whole issue of smart pointers search google for "c++ smart pointers"
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    There is no relationship between reference counting and std::auto_ptr, if that's what you mean by the second part of your question.

    Also, don't bump your threads. Especially not after just one hour.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Here's an excellent smart pointer implementation for beginners to learn from:
    http://www.josuttis.com/
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM
  4. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21