Created
November 7, 2009 17:31
-
-
Save icebreaker/228790 to your computer and use it in GitHub Desktop.
light-weight scoped pointer implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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