Re: Need design advice
From: Ivan Vecerina (please_use_web_form_at_ivan.vecerina.com)
Date: 11/06/03
- Next message: Alvaro Segura: "Objects in var. length arguments (?)"
- Previous message: Atz: "Re: Yet another SMS sender"
- In reply to: Patrick Kowalzick: "Re: Need design advice"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 6 Nov 2003 11:51:36 +0100
"Patrick Kowalzick" <Patrick.Kowalzick@cern.ch> wrote in message
news:bod0hv$5s9$1@sunnews.cern.ch...
| Do you mean here, that I avoid all the pifalls when I throw exceptions
| inside classes with pointers (allocating memory) when I just use a smart
| pointer? This in fact would be very nice :-).
Well, it can help with a lot of the issues. But smart pointers
themselves aren't always trivial to use either...
| Coming back to the topic.
|
| Assume my object exists e.g. a Point "p", and I have to pass it to another
| class "bar". I could do this via a proxy "bar_proxy". "bar" needs "p" for
| quite a long time, and I could not be sure, that "p" still exists, when
| "bar" needs it. What means, I need here a kind of "delayed copying", that
in
| case the Point "p" is destructed, the "bar_proxy" creates an instance.
[...]
There are a few possible approaches.
- first, avoid copies as much as possible by passing objects
by reference when possible.
- the objects themselves may choose to implement some form of
reference counting, with or without automated COW (copy-on-write).
There are many options really.
I would suggest studying Scott Meyer's "(More) Effective C++" books
regarding these topics. You may also read about std::string (which
currently is typically implemented without reference counting,
because it is complex and inefficient in multithreaded apps).
Also, consider using boost::shared_ptr (from www.boost.org)
as a smart pointer for the contained data.
What you really need to chose AFAICT is whether a type will
automatically share its data among object copies or not.
If not, the user is responsible to take care of lifetime issues.
If yes, you need to decide wheter this sharing will be
visible to the users or not (if not, COW needs to be implemented).
| For a huge amount of Data (1000dim Point ;-) ) I assume that the class
Point
| itself is smart enough to do this job, what means inside my "bar_proxy" I
| just create another "Point pp = p".
| For small data-types (double, int,....) I just create a copy as well.
|
| But what shall I do for "medium-sized" data-types ?
There is no general rule about which approach is better.
Testing and tuning is typically required -- as for all optimizations
attempts (avoiding data copies is an optimization...).
Regards,
Ivan
-- http://ivan.vecerina.com
- Next message: Alvaro Segura: "Objects in var. length arguments (?)"
- Previous message: Atz: "Re: Yet another SMS sender"
- In reply to: Patrick Kowalzick: "Re: Need design advice"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|