By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

pointless object copies

P: n/a
hi,

i would like to make some code that operates on sets of points - does
convex hulls and other operations. but it looks like i will need a lot
of pointless object copies:

================================================== ====================================
// point cloud class
class pc : public std::vector<vec2d>
{
// utility methods for operating on lists of points

static pc&
convex_hull(const pc& _1, const pc& _2)
{
// return the convex hull of these two clouds somehow
}
};

pc a = get_a();
pc b = get_b();
pc c = get_c();

pc hull_ab = pc::convex_hull(a, b); // pointless copies from
a and b
pc hull_bc = pc::convex_hull(b, c); // pointless copies from
b and c

pc::iterator alpha = b.begin();
pc::iterator omega = b.end();
pc b_on_both;

for(; alpha != omega; ++alpha )
if ( hull_ab.has(*alpha) && hull_bc.has(*alpha) )
b_on_both.push_back(*alpha); // pointless copies due
to operator=
================================================== ====================================

how do i avoid all these copies? obviously, there is a way - using
pointers. i could just make pc inherit from std::vector<vec2d*>. but in
books and on the web, i am told that containers of pointers are evil,
so i don't want to implement my class with one! is this a case where
'garbage collection' or 'reference counting' is necessary?

_jsnX

Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
jsnX wrote:
i would like to make some code that operates on sets of points - does
convex hulls and other operations. but it looks like i will need a lot
of pointless object copies:

================================================== ====================================
// point cloud class
class pc : public std::vector<vec2d>
{
// utility methods for operating on lists of points

static pc&
convex_hull(const pc& _1, const pc& _2)
{
// return the convex hull of these two clouds somehow
}
};

pc a = get_a();
pc b = get_b();
pc c = get_c();

pc hull_ab = pc::convex_hull(a, b); // pointless copies from
a and b
Copies? Where? The arguments are passed by reference to const. That
means that inside the function the _1 and _2 are *aliases* to the real
objects 'a' and 'b' here.
pc hull_bc = pc::convex_hull(b, c); // pointless copies from
b and c
Same question: what copies?
pc::iterator alpha = b.begin();
pc::iterator omega = b.end();
pc b_on_both;

for(; alpha != omega; ++alpha )
if ( hull_ab.has(*alpha) && hull_bc.has(*alpha) )
b_on_both.push_back(*alpha); // pointless copies due
to operator=
Well, to store objects in a container, you have to make a copy. If you
don't want to store a separate (read: independent, self-sufficient) piece
of information, declare a vector of pointers. It's dangerous since
pointers tend to easily become invalid, it's cumbersome because pointers
need care in handling, but it prevents "unnecessary" copying.
================================================== ====================================

how do i avoid all these copies? obviously, there is a way - using
pointers. i could just make pc inherit from std::vector<vec2d*>. but in
books and on the web, i am told that containers of pointers are evil,
so i don't want to implement my class with one! is this a case where
'garbage collection' or 'reference counting' is necessary?


You could have a container of smart (or shared) pointer objects, see
boost::shared_ptr. They can be stored in a container, they are relatively
safe and the count references IIRC.

V
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.