Skip to content

Instantly share code, notes, and snippets.

@icebreaker
Created November 7, 2009 17:31
Show Gist options
  • Select an option

  • Save icebreaker/228790 to your computer and use it in GitHub Desktop.

Select an option

Save icebreaker/228790 to your computer and use it in GitHub Desktop.
light-weight scoped pointer implementation
// Scoped Pointer
#include <stdio.h>
template<typename T>
class scoped_pointer
{
public:
scoped_pointer(T *pPointer=new T) : mPointer(pPointer) {}
virtual ~scoped_pointer()
{
delete mPointer;
}
operator T*() const
{
return mPointer;
}
private:
T *mPointer;
};
class test_class
{
public:
test_class()
{
printf("test class created\n");
}
virtual ~test_class(void)
{
printf("test class deleted\n");
}
void print(void)
{
printf("test class print\n");
}
};
void test_func(test_class *pParam);
int main(int argc, char *argv[])
{
printf("scoped pointer test begin\n\n");
test_func(scoped_pointer<test_class>());
printf("\nscoped pointer test end\n\n");
}
void test_func(test_class *pParam)
{
pParam->print();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment