2

I am creating my own implementation of a weak/strong reference pointer relationship and I am confused to the configuration. When I have a class that contains the strong reference, and I want to set the strong pointer to another class that has a weak reference, should I be passing in the pointer of a weak_ref pointer?

If someone could please take a look at this code and let me know I will appriciate it, also if you notice any other issues please let me know. I put the three files into codepad files so this page does not become cumbersome.

WeakReference.h : http://codepad.org/nNtRk4vO

StrongReference.h : http://codepad.org/MGi0fZ4J

Please do not turn this into a "use boost, use std, use tr1" argument, I am looking for help on this code, not using something else.

chadb
  • 1,138
  • 3
  • 13
  • 36
  • main.cpp : http://codepad.org/nNtRk4vO (This had to be done because I am a new user) – chadb Mar 14 '11 at 00:30
  • Is this a programming exercise? Or maybe homework? If not, why not use what is available in boost? – Sam Miller Mar 14 '11 at 01:16
  • This is a programming exercise, I have wanted to learn to do this for a while and I enjoy implementing a lot of my own stuff. Some may call it redundant because it it is already out there, but I find it fun. – chadb Mar 14 '11 at 03:30
  • not at all redundant, sometimes that is the best way to learn. I asked because some users will suggest boost without the homework tag. – Sam Miller Mar 14 '11 at 12:32

1 Answers1

1

The implementations that I have seen, and that I have worked on, that use the concepts of strong and weak reference, all use two counts. Sometimes one count is the number of weak references and the other is the number of strong. Other times one of the counts represents the sum of strong + weak references. Sometimes there are other schemes. But I have not yet seen an implementation with only one count as yours seems to have.

Perhaps it would help if you wrote a specification for exactly what the behavior for strong_ref and weak_ref are supposed to be. I find that the act of writing a specification can actually aid in the debugging of the implementation. It forces you to think about inputs, outputs, corner cases, invalid cases, etc.

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
  • When I was planning this I did a small spec, and it was basically just reference counting. To me it made since to have only one counter, as their should only be one strong_ref (owned by the person that owns it), and the rest should be weak_ref (those who depend on it). Is this how the typical relationship is? – chadb Mar 14 '11 at 03:32
  • In the C++0x (and boost) shared/weak_ptr model the shared_ptr's share ownership of the constructed state of the object, and of the allocation of the memory in which the object resides. The weak_ptrs share ownership only of the allocated memory (with the shared_ptrs). When you want to reference via a weak_ptr you first must convert it to a shared_ptr, which may fail if the object has already been destructed. – Howard Hinnant Mar 14 '11 at 14:31