473,466 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

pointless object copies

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
1 1442
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Wolfgang Jeltsch | last post by:
Hello, what is the best way to prevent the user of a class from making copies of objects of this class? Making the copy constructor private is a solution but I'm looking for a better one. ...
2
by: Aaron | last post by:
Hi, I've seen javascript code where a constructor function is passed an argument "document", and inside the function itself the assignment "this.document = document;" is made. This is the code...
7
by: Jeffrey E. Forcier | last post by:
I am attempting to write a class whose string representation changes in response to external stimuli. While that effect is obviously possible via other means, I attempted this method first and was...
5
by: Markus Dehmann | last post by:
Profiling has shown that during runtime of my program, 100 million objects of class X are constructed and destructed. What is the best way to optimize in such a case, both for memory and speed...
6
by: WayneD | last post by:
Hi All, Just got started in C#... Here's some C# code: public MyClass { private MyThingy m_Thingy;
2
by: films | last post by:
I understand the concept. Serialization of a class will add all the sub-objects of the class to the stream if there are also serializible. So say I have: class Author {
3
by: GoogleEyeJoe | last post by:
Dear ladies and gents, I'm trying to determine whether the .NET Framework implements a means of transactional processing without the need for a database. Essentially, I'd like to enlist...
1
by: falcon198198 | last post by:
Greetings: I was hoping for some advice on an application. Here is what I am working on: Basically I have a bigger application that is having a printing problem making multiple copies of a pdf...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.