automatically disassociating pointers

This is a discussion on automatically disassociating pointers within the C++ Programming forums, part of the General Programming Boards category; I was wondering, what is the official/more likely name of this? The first I thought it'd be called is "automatically ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    108

    automatically disassociating pointers

    I was wondering, what is the official/more likely name of this? The first I thought it'd be called is "automatically dereferencing smart poitners" but I guess that's wrong.

    This is what it is : a smart pointer template class, where if it's pointing to B, and B is destroyed (and then the destructor called etc), the pointer class would automatically set its contents to NULL (instead of &B).

    Does this exist, say, in boost library or anything like that? Otherwise, I'm thinking of my own implementation, but yeah, would be nice to have a good idea of how it's supposed to be done.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,267
    Perhaps you are thinking of something like std::tr1::weak_ptr?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    108
    is it standard c++? The first reference I got on the net was on msdn.. and it seems that you'll need a shared pointer to work with that?

  4. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    this may be relevant to your interests

    Reference counting - Wikipedia, the free encyclopedia

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,267
    Quote Originally Posted by underthesun
    is it standard c++?
    Not yet, which is why it is currently in the std::tr1 namespace rather than the std namespace.

    Quote Originally Posted by underthesun
    and it seems that you'll need a shared pointer to work with that?
    Yes. The shared_ptr(s) have ownership, the weak_ptr(s) do not, but are notified when there are no owners left.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    108
    Thanks..

    what I've done is made a special smart container class using a template, where the stuff contained has to be a subclass of this base class, which will call the container to dissociate when it gets destructed. I think this should be ok, it's just strange for this not covered by stl. I guess I might have been imagining things..

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,007
    Don't be surprised. It's been 10 years or so since the last C++ standard came out and a lot of things change in 10 years. Plus there's only so much time they have to make new additions to the library.
    Hopefully, this will pick up pace as the committee is exploring making Technical Reports with new additions to the standard library which compiler vendors can implement on a shorter period of time. They are then integrated into the main standard once its next revision comes around.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    108
    Hmm, you are right. And then it'll take time for g++ to support all these new stuff, and even more time for it to be fully robust in supporting them I guess..

    But while making that special pointer class I just couldn't help but think how nicely all the c++ stuff, those templates and operator overloading fit together.. Bjarne Stroustrup (sic?) is truly a genius.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,007
    Ah, don't lay all the praise on him. He is just the founder of the language.
    You should thank the C++ committee and all of the people who help out by suggesting changes.
    And C++ still has its problems. Templates are very tricky stuff and there are still some things missing in that area...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    108
    Ah that's true, fair enough. But without him, c++ wouldn't even be here.. how much did it end up changing from his initial vision anyways?

    btw is there a way to have a "label" as a template argument? Would rather use them than #define macros..

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,007
    Quote Originally Posted by underthesun View Post
    Ah that's true, fair enough. But without him, c++ wouldn't even be here.. how much did it end up changing from his initial vision anyways?
    I think he's pretty happy with how it has evolved. There may have been a few things he'd rather see, but the committee lacks the funding to do.
    So yes, praise be to him, but not all of it

    btw is there a way to have a "label" as a template argument? Would rather use them than #define macros..
    I'm not sure what you mean.
    This is possible:
    Code:
    const int x = 10;
    template<int N> void myfunc()
    {
        std::cout << N << std::endl;
    }
    If not this, then I don't know what you mean...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,267
    Quote Originally Posted by underthesun
    btw is there a way to have a "label" as a template argument? Would rather use them than #define macros..
    What do you have in mind? You might want to give an example of the macro that you intend to do away with.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by underthesun View Post
    what I've done is made a special smart container class using a template, where the stuff contained has to be a subclass of this base class, which will call the container to dissociate when it gets destructed. I think this should be ok, it's just strange for this not covered by stl.
    I distrust code that requires such shenanigans. Who holds this pointer, and why wouldn't he know that the pointee is gone through some other means? In a perfect ownership model, it should be technically impossible for an object to be destroyed while an observer holds a reference to it.

    Also, given that your pointer is an observing pointer and it's usually quite possible that there's multiple observers for a single object, does that mean that the base class must hold a list of every observer?
    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

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    108
    Yeah, it does hold a list of observers, using a list<Holder*>. The template class has a holder inside it. i.e
    Code:
    class Pointee{
       list<PointerHolder*> holders;
       // plus destructor methods that dissociates this
    };
    
    class PointerHolder{
       Pointee * thepointed;
       // plus destructor methods that dissociates with the pointed pointees
    };
    
    template <class SubclassOfPointee>
    class Holder{
       PointerHolder holder;
       // plus a bunch of convenient operators such as:
       Holder<SubclassOfPointee>& operator=(SubclassOfPointee * thepointee){
          holder.assign(thepointee);
          return *this;
       }
    };
    That list of pointers might seem overkill, but I was going to implement it that way for what I needed for keeping track of a bunch of enemies in a game, where they could die any second, and these observers do not own the pointer in any way.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    108
    What do you have in mind? You might want to give an example of the macro that you intend to do away with.
    Was thinking of allowing
    Code:
    template <class M, label moo>
    void do100times(M& m){
       m.moo();
    }
    Then again you can probably do most of that with #define macros, except they seem a bit messy.. sorry was going off topic with the thread.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  2. Pointers to function(function pointers)
    By abhishek_charli in forum C Programming
    Replies: 4
    Last Post: 06-23-2002, 02:24 AM
  3. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 07:53 PM
  4. Pointers pointers pointers...
    By SMurf in forum C Programming
    Replies: 8
    Last Post: 10-23-2001, 05:55 PM

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