What's a smart pointer?

G

Gernot Frisch

and what for would I need it?

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
 
S

Sharad Kala

Gernot Frisch said:
and what for would I need it?

It is a simple wrapper around a regular pointer. Typically operator * and ->
are overloaded in such a class. One very simple example would be like -
template <class T>
class SPtr
{
T* ptr;
public:
SPtr(T* p = 0) : ptr(p) {}
~SPtr() {delete ptr;}
T& operator*() {return *ptr;}
T* operator->() {return ptr;}
// ...
};


They give feel like raw (or dumb) pointers but one can give semantics to
- Their construction and destruction
- Copying and assignment
- Dereferencing etc

The topic can be best described only by a good text.
Some good references to smart pointers are
- More effective C++ (Meyers)
- Modern C++ design (Alexandrescu)
- Boost smart pointers

Sharad
 
P

PKH

Sharad Kala said:
It is a simple wrapper around a regular pointer. Typically operator *
and ->
are overloaded in such a class. One very simple example would be like -
template <class T>
class SPtr
{
T* ptr;
public:
SPtr(T* p = 0) : ptr(p) {}
~SPtr() {delete ptr;}
T& operator*() {return *ptr;}
T* operator->() {return ptr;}
// ...
};


They give feel like raw (or dumb) pointers but one can give semantics to
- Their construction and destruction
- Copying and assignment
- Dereferencing etc

The topic can be best described only by a good text.
Some good references to smart pointers are
- More effective C++ (Meyers)
- Modern C++ design (Alexandrescu)
- Boost smart pointers

Sharad

They are very useful if you integrate them with the delete-operator, so that
when deleting memory, all smartpointers that point to that memory will be
invalidated by setting their object-pointers to NULL.
By using a hashtable for memory locations with smartpointers, the
performance hit is low.

PKH
 
M

Method Man

PKH said:
They are very useful if you integrate them with the delete-operator, so that
when deleting memory, all smartpointers that point to that memory will be
invalidated by setting their object-pointers to NULL.
By using a hashtable for memory locations with smartpointers, the
performance hit is low.

PKH

So are 'handles' equivalent to smart pointers created/managed by the OS?
 
M

Method Man

Ioannis Vranos said:
When you are saying handles, are you talking about .NET/CLI?

I was being very vague, but I guess I was referring to handles in Win32
programming (handle to a window) or COM objects. My knowledge of handles is
very little, but they sound exactly like smart pointers to me.
 
P

PKH

Method Man said:
I was being very vague, but I guess I was referring to handles in Win32
programming (handle to a window) or COM objects. My knowledge of handles
is
very little, but they sound exactly like smart pointers to me.

It's not quite the same. Handles in Win32 use reference counting to figure
out how many has pointers to the data and only releases when the reference
count reaches 0.
With smartpointers, an allocation can have many smartpointers pointing to it
when it is deleted. When integrated with delete, all these smartpointers
will be invalidated. I.E. using f.ex pcSmartPtr->GetObject() now returns
NULL for all of them. When using smartpointers you allways have to use a
function like GetObject and check the returnvalue when accessing data.

PKH
 
H

Howard

PKH said:
It's not quite the same. Handles in Win32 use reference counting to figure
out how many has pointers to the data and only releases when the reference
count reaches 0.

While you're correct that that is how handles are *used* in Windows, the
HANDLE data type itself, in C++ (and C) under Windows, is defined as void*.
It's how you obtain, release, and use them that makes the difference. (It
would be a mistake, for example, to use new and delete on one.) But the
object itself is just a pointer to void.

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

int(a) vs (int)a 19
pointer to function 4
a[3} slower than a.x; a.z; a.z 27
a thread safe stack? 7
reference to parent inherritance? 9
queue where I can delete in the middle? 3
unreachable code? 4
date comparison 2

Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top