I have class SmartPointer, Date and ReferenceCounter. I have task: To be able to check the value of the counter inside the smart pointer count, add a method useCount()that returns the counter count.
So, this my code, where can i add useCount?
useCount() Should be put into smart pointer as a separate variable and getter function, this will tell how many smart pointer object own the resource.
However since your smart pointer acts as wrapper around invasive reference count, you should also have GetRefCount() in ReferenceCounter class, because your design implies derived classes from ReferenceCounter don't need smart pointer.
therefore useCount != RefCount
EDIT:
Unless you don't plan to derive from ReferenceCounter? In which case ReferenceCounter would better be placed into smart pointer as nested class.
No it wont work, getCount() job is to return ownership count not to increment or decrement owned object.
You need to make SmartPointer a friend to referenceCounter to avoid writing 2 getcount() functions, that's why it would be the best to have it as private nested class instead and make count variable public.
class ReferenceCount adds no value over int. In fact, it's buggy because the preincrement and predecrement operations return the values normally associated with post increment/decrement!
SmartPointer::operator==() is doing an assignment, not a comparison.
Also, assuming that should be SmartPointer::operator=(), you should change it to increment sp->ref before you decrement this->ref, just in case they somehow point to the same ref. Trust me, that's a bug you don't want to have.... :)
SmartPointer<T>::SmartPointer(T *X, ReferenceCounter* ref) creates a new ReferenceCounter for this, but it increments the argument ref.
Why does useCount() return a Date?
Why does SmartPointer have a constructor that takes a Date? It's a template, shouldn't anything related to a specific type like Date be covered by SmartPointer<Date> instead?