Smart Pointers

Nowhere documentated lie the smart pointers. I read quite a lot of topics about them, but I never saw a snippet of code. If I understand correctly, they should be classes defined with a template, containing a pointer to the templated type or class. I was playing with this concept and came up with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

template<class T>
class SmPointer
{
    T* data;
public:
    inline  SmPointer():      data(0)   {};
    inline  SmPointer(T* t): data(t)  {};
    inline ~SmPointer() {delete data;data=0;};

    operator T*() {return data;};
};
/*
 * 
 */
int main()
{
    SmPointer<int> Bla = new int(4);
    std::cout << Bla;
    return 0;
}


Am I making any grave mistakes here and are there things to improve upon, am I doing good or am I wrong all along?


P.S.: It's 18 past midnight here. I just officially turned 16! :D
Last edited on
For smart pointers look up boost smart pointer library, and std::auto_ptr.

It's 18 past midnight here. I just officially turned 16! :D

Happy birthday!
Am I making any grave mistakes here


Yes.

You're sharing ownership, so each owner will delete the same pointer, resulting in multiple deletes.

Consider the following:

1
2
3
4
5
6
7
8
{
  SmPointer<int> a(new int);  // allocated
  {
    SmPointer<int> b(a);  // copied to 'b'
  }  // b's dtor deleted the int here

  *a = 5;  // EXPLODE, the int has already been deleted by b
} // EXPLODE, 'a' deletes the pointer again, after 'b' already deleted it 


I'd come up with a more complete example, but I'm at work now so I can't! Maybe when I get home.
here's the basic idea behind a reference counting smart pointer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
template <typename T>
class SmPointer
{
private:
  T* ptr;  // the actual data we're pointing to
  unsigned* ref; // reference count keeping track of all SmPointer's accessing this data

  // a function to decrement the reference count
  void DecRef()
  {
    if(ref)
    {
      --(*ref);  // dec the ref
      if(!*ref)  // if it's zero
      {
        // no more Smart Pointers using this data, so clean up
        delete ptr;
        delete ref;
      }
    }
  }

public:
  SmPointer() : ptr(0), ref(0) { } // default ctor

  SmPointer(const SmPointer<T>& r)  // copy ctor
    : ptr(r.ptr), ref(r.ref)
  {
    // increment the ref count
    if(ref)
      ++(*ref);
  }

  explicit SmPointer(T* p)  // ctor from a pointer
    : ptr(p), ref( new unsigned(1) )  // start the ref count at 1
  { }

  SmPointer<T>& operator = (const SmPointer<T>& r)  // assignment operator
  {
    // abandon the previous pointer by decreasing the ref
    DecRef();

    // then adapt this new pointer
    ptr = r.ptr;
    ref = r.ref;

    if(ref)
      ++(*ref);

    return *this;
  }

  ~SmPointer()  // dtor
  {
    // abandon by decreasing the ref
    DecRef();
  }
};
Thanks for the replies Disch, your codes always impress me. I just have a few questions, what is the explicit keyword, is unsigned by it's self a type and what are it's limits?
what is the explicit keyword

Explicit keyword forces to use explicit constructor syntax:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Explicit
{
  int x;
  public:
    explicit Explicit(int){}
};

class NonExplicit
{
  int x;
  public:
     NonExplicit(int){}
};


int main()
{

NonExplicit nexp_good(10);      // Calling constructor using explicit syntax
NonExplicit nexp_also_good=10;  //Calling constructor using implicit syntax

Explicit exp_good(10); //  using explicit syntax
Explicit exp_bad=10;   // Compile error! (because explicit keyword is used)


}

http://www.glenmccl.com/tip_023.htm

is unsigned by it's self a type and what are it's limits?
unsigned is the same as unsigned int
Reference counter is not the only way
http://ootips.org/yonat/4dev/smart-pointers.html#WhyBugs
explicit is mainly to prevent you from accidentally reassigning a pointer to a smart pointer that you didn't mean to. Typically via a function call:

1
2
3
4
5
6
7
8
9
10
11
12
13
void AFunction(SmPointer<int> p)
{
  // ...
}


int main()
{
  int myint = 5;
  AFunction( &myint );  // compiler errors with explicit keyword
     // compiles just fine without it, but the program will explode because the smart
     //  pointer will try to delete myint
}
Topic archived. No new replies allowed.