C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

where to read about smart pointers

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Marlene Miller
Guest





PostPosted: Wed Jan 26, 2005 10:12 am    Post subject: where to read about smart pointers Reply with quote



I request some guidance on how to learn about smart pointers.

1. Modern C++ Design by Andrei Alexandrescu, chapter 7 Smart Pointers. Is
that a good place to *start*? For me, MC++D is very advanced, but I think I
could cope. Is it essential or *wise* or convenient to read chapters 1-6
before reading chapter 7?

2. More Effective C++ by Scott Meyers. I could lookup "smart pointers" in
the index, follow the page references and read every related Item.

3. I don't have Herb Sutter's books (yet).

4. Is there something good that I should read that is not as advanced in
preparation for MC++D? I've studied Accelerated C++, that's all I know about
reference counting.

5. I'd like to know if I am on the right track. Classes, regarded as an OO
concept rather than a language construct, have association relationships.
In C++ I suppose an association is implemented as a data member subobject or
a data member pointer or reference to an object of class type. If object X
has a pointer to object Y, X could delete Y. Not good, if X does not own Y.
If X has a reference to Y, and if Y is deleted, X needs to know. In Java, I
never worried about this. As long as some object has a reference to Y, Y
will not be garbage collected. Class associations are a basic concept.
Knowing how to implement associations must also be basic. Knowing how the
client object navigates to the service object without the power to delete it
must be basic. What is the common/default idiom/technique to manage class
associations in C++?

Thank you.
- Marlene


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Maxim Yegorushkin
Guest





PostPosted: Thu Jan 27, 2005 8:58 pm    Post subject: Re: where to read about smart pointers Reply with quote



Marlene Miller wrote:

Quote:
I request some guidance on how to learn about smart pointers.

1. Modern C++ Design by Andrei Alexandrescu, chapter 7 Smart Pointers. Is
that a good place to *start*? For me, MC++D is very advanced, but I
think I
could cope. Is it essential or *wise* or convenient to read chapters 1-6
before reading chapter 7?

I think it's beneficial to read the whole book because, IIRC, chapters are
based on previous chapters. Anyway you or your friend paid for it, why
waste it? ;)

Quote:
2. More Effective C++ by Scott Meyers. I could lookup "smart pointers" in
the index, follow the page references and read every related Item.

This might help, but... Meyers books are pretty simple to novices and
concepts described there and understanding gained will pay you back for
sure (of course if you are concerned about your CS career rather than
passing an exam).

Quote:
3. I don't have Herb Sutter's books (yet).

Grab it, I mean, if you are really interested in C++, get every mainstream
C++ book and read it. The books I would recommend and I would read them
myself again in the following order if I had to start again:

* The C++ Programming Language by Stroustrup (I had to read it 4 times in
1.5-year period to comprehend and appreciate)
* C++ for Real Programmers by Jeff Alger (a little bit outdated by still
makes sense)
* Effective and More Effective C++ by Scott Meyers
(following will get you started about applied OOP before digging deeper)
* Design Pattern by Erich Gamma, et al
* Object-Oriented Analysis and Design with Applications by Grady Booch
(digging deeper in C++)
* Exceptional's by Herb Sutter
* Modern Design C++ by Andrei Alexandrescu
* C++ Gotchas by Stephen C. Dewhurst

[]

Quote:
5. I'd like to know if I am on the right track. Classes, regarded as an
OO concept rather than a language construct, have association
relationships. In C++ I suppose an association is implemented as a data
member subobject or a data member pointer or reference to an object of
class type.

The problem is that C++ does not have a direct mapping to OOP(UML) or vice
versa.

OOP aggregation is most often expressed in C++ as a class having a member
of that type (the member is destroyed in the class's destructor) or having
a pointer of that type that is deleted whenever the class's designer
wishes it.

This is the strict ownership concept concept in C++ - who owns the object
is he who deletes it, but not vice versa. The shared ownership concept in
C++ is - whoever releases its reference to an object last also he who
destroys the object, but it neither owns it nor knows that it is the sole
referrer at the moment.

An association in C++ has no direct representation at all. The OOP concept
of association in C++ may be represented as having a reference(pointer) to
a class, or by a member function of a class taking a pointer(reference) to
a referenced class and using that pointer by calling member functions
through that pointer. Most othen, association in C++ means that a class
has an access to a declarataion of the referenced class (includes a header
file of it) but does not owns an object of it, or uses shared ownership.

Quote:
If object X has a pointer to object Y, X could delete Y. Not good, if X
does not own Y.

It depends on ownership. If X ownes Y it can delete it whenever it wants
to.

Quote:
If X has a reference to Y, and if Y is deleted, X needs to know.

If it happens in C++ - it's a mess. In such cases they use shared
ownership - somebody releases its reference to Y but you don't care,
because as long as you use Y you hold a reference to it, and as long as Y
has a reference it's not deleted.

Quote:
In Java, I
never worried about this.As long as some object has a reference to Y, Y
will not be garbage collected. Class associations are a basic concept.

Java hides the ownership and memory management concepts from developerz.

--
Maxim Yegorushkin

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Patrick Leslie Polzer
Guest





PostPosted: Thu Jan 27, 2005 9:14 pm    Post subject: Re: where to read about smart pointers Reply with quote



Marlene Miller wrote:

Quote:
In C++ I suppose an association is implemented as a data member subobject or
a data member pointer or reference to an object of class type. If object X
has a pointer to object Y, X could delete Y. Not good, if X does not own Y.

Make it const then. Pointers to const data cannot be deleted.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
David Abrahams
Guest





PostPosted: Fri Jan 28, 2005 12:37 am    Post subject: Re: where to read about smart pointers Reply with quote

"Marlene Miller" <marlenemiller (AT) worldnet (DOT) att.net> writes:

Quote:
I request some guidance on how to learn about smart pointers.

1. Modern C++ Design by Andrei Alexandrescu, chapter 7 Smart Pointers. Is
that a good place to *start*? For me, MC++D is very advanced, but I think I
could cope. Is it essential or *wise* or convenient to read chapters 1-6
before reading chapter 7?

Well, I think MC++D is a very instructive book, but it's not
particularly well-focused on smart pointers. There's a lot of
material about how to write fancy libraries that probably doesn't
directly address what you're interested in, and since much of that
material is used in the discussion of Andrei's smart pointer design,
you will probably have to invest quite a lot just to get the
smart-pointer-specific information out of it. You could probably learn
quite a lot very quickly about smart pointers by reading
http://www.boost.org/libs/smart_ptr/smart_ptr.htm and following the
links there. Be sure not to miss
http://www.boost.org/libs/smart_ptr/sp_techniques.html; it's a gold
mine! It's not quite comprehensive but it's a very good start.
You might follow that with http://home.comcast.net/~jturkanis/move_ptr
(if can stand slow websites ;->).

Quote:
2. More Effective C++ by Scott Meyers. I could lookup "smart pointers" in
the index, follow the page references and read every related Item.

You can learn quite a lot that way. However, be sure you check his
errata pages, e.g. http://www.aristeia.com/BookErrata/M29Source_frames.html

Quote:
3. I don't have Herb Sutter's books (yet).

4. Is there something good that I should read that is not as
advanced in preparation for MC++D? I've studied Accelerated C++,
that's all I know about reference counting.

I think Meyers is a good way to go.

Quote:
5. I'd like to know if I am on the right track. Classes, regarded as an OO
concept rather than a language construct, have association relationships.
In C++ I suppose an association is implemented as a data member subobject or
a data member pointer or reference to an object of class type. If object X
has a pointer to object Y, X could delete Y. Not good, if X does not own Y.
If X has a reference to Y, and if Y is deleted, X needs to know.

I think the only place mentioned so far that you'll learn how to
handle this in C++ is in the boost docs, particularly looking at
weak_ptr.

Quote:
In Java, I never worried about this. As long as some object has a
reference to Y, Y will not be garbage collected. Class associations
are a basic concept. Knowing how to implement associations must
also be basic. Knowing how the client object navigates to the
service object without the power to delete it must be basic. What is
the common/default idiom/technique to manage class associations in
C++?

I think most people tend to use raw pointers, which simply dangle when
the target object is deleted. That's more error-prone than using
weak_ptr, but, of course, it's easier and sometimes more efficient.

HTH,

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Allan W
Guest





PostPosted: Fri Jan 28, 2005 12:39 am    Post subject: Re: where to read about smart pointers Reply with quote

Patrick Leslie Polzer wrote:
Quote:
Pointers to const data cannot be deleted.
Why do you say this?


Try it. It's perfectly legal.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Marlene Miller
Guest





PostPosted: Fri Jan 28, 2005 11:23 am    Post subject: Re: where to read about smart pointers Reply with quote

Thank you David.

Quote:
I think most people tend to use raw pointers, which simply dangle when
the target object is deleted. That's more error-prone than using
weak_ptr, but, of course, it's easier and sometimes more efficient.

Suppose you have an application that is composed of layers. Layer 1 manages
some resources. Layer 2 requests access to a resource. Could it happen that
L1 has to give L2 a pointer to one of its objects, then L2 deletes the
object? Do you ever need to protect your objects?


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ivan Vecerina
Guest





PostPosted: Sat Jan 29, 2005 4:24 am    Post subject: Re: where to read about smart pointers Reply with quote

"Marlene Miller" <marlenemiller (AT) worldnet (DOT) att.net> wrote

Quote:
I think most people tend to use raw pointers, which simply dangle when
the target object is deleted. That's more error-prone than using
weak_ptr, but, of course, it's easier and sometimes more efficient.

Suppose you have an application that is composed of layers. Layer 1
manages
some resources. Layer 2 requests access to a resource. Could it happen
that
L1 has to give L2 a pointer to one of its objects, then L2 deletes the
object? Do you ever need to protect your objects?

You can't totally protect you objects in C++.
But you can go a long way towards preventing programming errors.

First of all, L1 should not pass an object pointer to L2,
but a reference (or some smart pointer like weak or shared_ptr).
This will strongly hint that L2 is not to delete the object.

Next, instead of providing access to the full object implementation
to L2, L1 could only provide access to a base class that has
a protected destructor, which also helps prevent accidental
destruction.

Example design of L1:

///////////////////////////////////
// L1.h

class L1Resource
{
public:
virtual void doSomething()=0;
//...
protected:
~L1Resource() {}
};

L1Resource& accessObject(); // returns an object


///////////////////////////////////
// L1.cpp

class L1Resource_Impl : public L1Resource
{
public:
virtual void doSomething() {....implementation....}
};

L1Resource& accessObject()
{
static L1Resource_Impl obj; // (simplification)

return obj;
}




I hope this helps,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
David Abrahams
Guest





PostPosted: Sat Jan 29, 2005 4:47 am    Post subject: Re: where to read about smart pointers Reply with quote

"Marlene Miller" <marlenemiller (AT) worldnet (DOT) att.net> writes:

Quote:
Thank you David.

I think most people tend to use raw pointers, which simply dangle when
the target object is deleted. That's more error-prone than using
weak_ptr, but, of course, it's easier and sometimes more efficient.

Suppose you have an application that is composed of layers. Layer 1 manages
some resources. Layer 2 requests access to a resource. Could it happen that
L1 has to give L2 a pointer to one of its objects, then L2 deletes the
object?

It could, but that's why we have smart pointers and tend to eschew
delete. Use auto_ptr when you want to transfer ownership from L1 to
L2. Use boost::shared_ptr when you essentially want garbage
collection -- the resource only disappears when everyone is done with
it.

Quote:
Do you ever need to protect your objects?

What do you mean when you say "protect?"

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Marlene Miller
Guest





PostPosted: Sat Jan 29, 2005 9:35 am    Post subject: Re: where to read about smart pointers Reply with quote

Thank you Ivan.

Quote:
First of all, L1 should not pass an object pointer to L2,
but a reference (or some smart pointer like weak or shared_ptr).
This will strongly hint that L2 is not to delete the object.

If the resource provider L1 gives out a reference to one of its objects, L1
should not delete that object unless it informs all the clients. Maybe L1
could give out objects that know where the real resource object is. Then L1
and L2 can delete their objects without concern for the other.

(I am trying to learn the common ways C++ is used to implement
associations.)


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ivan Vecerina
Guest





PostPosted: Sat Jan 29, 2005 3:26 pm    Post subject: Re: where to read about smart pointers Reply with quote

"Marlene Miller" <marlenemiller (AT) worldnet (DOT) att.net> wrote

Quote:
If the resource provider L1 gives out a reference to one of its objects,
L1
should not delete that object unless it informs all the clients. Maybe L1
could give out objects that know where the real resource object is. Then
L1
and L2 can delete their objects without concern for the other.

(I am trying to learn the common ways C++ is used to implement
associations.)

It is a common idiom is to expose only a Handle (or Handle-like class)
that maintains a reference to the actual resource, and will gracefully
fail if the resource was released by the provider.
std::weak_ptr could be used as such a 'weak handle'. The interface of
boost::weak_ptr is even designed to be compatible with a multi-threaded
environment, as it requires 'locking' the object (with a shared_ptr)
before being able to access it. See the usage notes in the Boost
library docs: http://www.boost.org/libs/smart_ptr/weak_ptr.htm

Essentially, the strategy will depend on which module, by design, has
the ownership of the resources. Other specific requirements, such
as the need to serialize access to the objects themselves, may also
be determinant.


This said, using shared_ptr may be the easiest solution if you want
the object to only be deleted when both the provided (L1) and the
user (L2) are done with it.


Cheers,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Marlene Miller
Guest





PostPosted: Sun Jan 30, 2005 4:09 am    Post subject: Re: where to read about smart pointers Reply with quote

Quote:
Do you ever need to protect your objects?

What do you mean when you say "protect?"

Your question might be an answer to my question. Protecting objects from
being deleted by objects in other parts of an application must not be an
ordinary concern. Even when the application is distributed.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.