473,803 Members | 3,030 Online
Bytes | Software Development & Data Engineering Community
+ 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<vec 2d>
{
// utility methods for operating on lists of points

static pc&
convex_hull(con st 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(*al pha) && hull_bc.has(*al pha) )
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<vec 2d*>. 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 1459
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<vec 2d>
{
// utility methods for operating on lists of points

static pc&
convex_hull(con st 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(*al pha) && hull_bc.has(*al pha) )
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 "unnecessar y" copying.
=============== =============== =============== =============== =============== ===========

how do i avoid all these copies? obviously, there is a way - using
pointers. i could just make pc inherit from std::vector<vec 2d*>. 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_p tr. 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
2498
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. Thanks, Wolfgang
2
1954
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 (or the part necessary for the example): function ToggleButton(document) { ToggleButton.images = new Array(4); for(i=0;i<4;i++) { ToggleButton.images = new
7
1913
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 surprised when it didn't work, so I now want to know why :) Given the following class definition: class MyClass(object): def Edit(self):
5
1938
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 (speed maybe more important)? One idea is to make the class very small, to contain only the most important member data, and put everything else, including member functions, into another class:
6
1472
by: WayneD | last post by:
Hi All, Just got started in C#... Here's some C# code: public MyClass { private MyThingy m_Thingy;
2
8891
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
3169
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 light-weight portable business objects within transactions so that that have the ability to roll-back if the client of the object wishes to undo any changes made. For example, if I had a, let's say for sake of simplicity, a person class that had two...
1
1240
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 so I set off to write a program that can watch a folder using filesystemwatcher. From the events that this generates I created a class and instantiated an object that will hold information like full path and last write time. I want to be able to...
275
12425
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
9564
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10310
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10292
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9121
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7603
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6841
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5498
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.