1 \section{\class{wxScopedPtr
}}\label{wxscopedptr
}
3 This is a simple scoped smart pointer implementation that is similar to
4 the
\urlref{Boost
}{http://www.boost.org
} smart pointers but rewritten to
9 Below is an example of using a wxWindows scoped smart pointer and
13 class MyClass
{ /* ... */
};
15 // declare a smart pointer to a MyClass called wxMyClassPtr
16 wxDECLARE_SCOPED_PTR(MyClass, wxMyClassPtr)
17 // declare a smart pointer to an array of chars
18 wxDECLARE_SCOPED_ARRAY(char, wxCharArray)
22 // define the first pointer class, must be complete
23 wxDEFINE_SCOPED_PTR(MyClass, wxMyClassPtr)
24 // define the second pointer class
25 wxDEFINE_SCOPED_ARRAY(char, wxCharArray)
27 // create an object with a new pointer to MyClass
28 wxMyClassPtr theObj(new MyClass());
29 // reset the pointer (deletes the previous one)
30 theObj.reset(new MyClass());
35 // create an object with a new array of chars
36 wxCharArray theCharObj(new char
[100]);
42 \wxheading{Declaring new smart pointer types
}
45 wxDECLAR_SCOPED_PTR( TYPE, // type of the values
46 CLASSNAME ); // name of the class
49 A smart pointer holds a pointer to an object (which must be complete
50 when wxDEFINE
\_SCOPED\_PTR() is called). The memory used by the object is
51 deleted when the smart pointer goes out of scope. The first argument
52 of the macro is the pointer type, the second is the name of the new
53 smart pointer class being created. Below we will use wxScopedPtr to
54 represent the scoped pointer class, but the user may create the class with any
57 \wxheading{Include files
}
63 \helpref{wxScopedArray
}{wxscopedarray
}\rtfsp
65 \latexignore{\rtfignore{\wxheading{Members
}}}
67 \membersection{wxScopedPtr::wxScopedPtr
}
69 \func{}{wxScopedPtr
}{\param{type
}{ * T = NULL
}}
71 Creates the smart pointer with the given pointer or none if NULL. On
72 compilers that support it, this uses the explicit keyword.
74 \membersection{wxScopedPtr::reset
}
76 \func{\void}{reset
}{\param{T
}{ p * = NULL
}}
78 Deletes the currently held pointer and sets it to 'p' or to NULL if no
79 arguments are specified. This function does check to make sure that the
80 pointer you are assigning is not the same pointer that is already stored.
82 \membersection{wxScopedPtr::operator *
}
84 \func{const T\&
}{operator *
}{\void}
86 This operator works like the standard C++ pointer operator to return the object
87 being pointed to by the pointer. If the pointer is NULL or invalid this will
90 \membersection{wxScopedPtr::operator -$>$
} % TODO
92 \func{const T*
}{operator -$>$
}{\void} % TODO
94 This operator works like the standard C++ pointer operator to return the pointer
95 in the smart pointer or NULL if it is empty.
97 \membersection{wxScopedPtr::get
}
99 \func{const T*
}{get
}{\void}
101 This operator gets the pointer stored in the smart pointer or returns NULL if
104 \membersection{wxScopedPtr::swap
}
106 \func{\void}{swap
}{\param{wxScopedPtr
}{ \& ot
}}
108 Swap the pointer inside the smart pointer with 'ot'. The pointer being swapped
109 must be of the same type (hence the same class name).