473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

what are smart pointers?

what r smart pointers?

Sep 27 '06 #1
5 1912
Bhan wrote:
what r smart pointers?
Something that looks like pointers but smarter. An example is
std::auto_ptr<T>, which looks like a pointer in that you can use
operator -and * to dereference it. In addition, it defines ownership
and ownership transfer, and does automatic clean up when an exception is
throw. These make it smarter than raw pointers.

Ben
Sep 27 '06 #2
Bhan wrote:
what r smart pointers?
See this explanation and development of the idea by A. Alexandrescu:

http://www.informit.com/articles/pri...p?p=25264&rl=1

See also this FAQ and the ones following for some simple examples:

http://www.parashift.com/c++-faq-lit...html#faq-16.22

Cheers! --M

Sep 27 '06 #3

Bhan wrote:
what r smart pointers?
Generally a smart pointer is not a pointer but an object that contains
and controls a pointer.

A smart pointer will have operator-and operator* overloaded so it
looks like a pointer.

Sometimes, but not all of the time, the smart-pointer will take care of
deleting the pointer for you.

Often you'll be able to construct a smart_ptr< T from a smart_ptr< U
whenever you can construct a T* from a U* without any casting, i.e. if U is derived from T or if T is const U. It will also be forbidden when the conversion is forbidden.
Beyond that though there is no covariance between the two so you cannot
have a return type of smart_ptr<Ufrom a derived class that is
supposed to return smart_ptr<T>. A smart_ptr<Uis not "a kind of"
smart_ptr<Teither so if you tried casting the reference it would be
an invalid cast.

Beware of const. const smart_ptr<T& is not a reference to an
immutable T but is an a reference to a const smart_ptr i.e. it's more
like a T* const. The equivalent of const T * (or T const *) would be a
smart_ptr< const T >. Even if the smart pointer has been written to be
"deep-const" you can often override that by duplicating the smart
pointer (if it is copyable) which will give you a non-const one.

Sep 27 '06 #4
Bhan wrote:
what r smart pointers?
Here are some examples:

boost::shared_ptr
std::auto_ptr
at::Ptr
Sep 28 '06 #5
Thanks to all

Sep 28 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: christopher diggins | last post by:
I would like to survey how widespread the usage of smart pointers in C++ code is today. Any anecdotal experience about the frequency of usage of smart pointer for dynamic allocation in your own...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
5
by: Matthias Kaeppler | last post by:
Hi, I was wondering, since STL containers are based around copying, whether it's a good idea to use reference counted smart pointers, such as boost::shared_ptr in STL containers. I can't store...
8
by: Axter | last post by:
I normally use a program call Doxygen to document my source code.(http://www.stack.nl/~dimitri/doxygen) This method works great for small and medium size projects, and you can get good...
16
by: Yong Hu | last post by:
Does the smart pointer never lead to memory leak? Thanks,
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
2
by: Steven T. Hatton | last post by:
It's my understanding that intrusive pointers are frowned upon. For example this is from the boost::intrusive_ptr documentation: "As a general rule, if it isn't obvious whether intrusive_ptr...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.