I will describe what I want using the vector class and then point out the limitations of using vector.
vector <MyClass> MyContainer.
MyContainer.pus h( new MyClass(...) )
MyContainer[0] now contains the first instance of MyClass.
MyContainer.pus h( new MyClass(...) )
MyContainer[1] now contains the second instance of MyClass.
MyContainer[1].delete() destroys the second instance of MyClass.
MyContainer[0] still contains the instance of MyClass.
MyContainer.pus h( new MyClass(...) )
MyContainer[2] now contains the third instance of MyClass. MyContainer[0] still contains the first instance of MyClass.
I would be perfectly happy with this (and might end up using this solution) except for the memory leak. In practice this memory leak would be a non-issue. Creation and destruction of MyClass is a significant undertaking and won't happen more than once a second even in extreme cases. Hence, the vector will grow by at most 24*60*60 or roughly 100,000 a day - and my program is not intended to run for days. Nonetheless, the leak is ugly and I would prefer a cleaner solution.
I need an integer index to the instances of MyClass because I need to be able to pass those instances back and forth to a non-C environment. In the other environment, I need to be able to track these instances of MyClass as integers as that is the only type that I have.
Another option is to cast some c++ pointer to an int, pass it back to the non-C environment and then cast that integer back to a c++ pointer when it comes back to the c++ side. That, IMHO, is even uglier.
By the way, I will use smart pointers instead of new and delete, but that is not relevant to this discussion.
I look forward to your suggestions.
Thanks,
Ken
1 1359 Banfa 9,065
Recognized Expert Moderator Expert
Use a map instead? You can't access using the [] operator but you can use a non-contiguous set of integers as the key to the objects and you can find a object given it's integer key.
Instead of calling the delete() member function of your class you can call the erase member function of the map. You may have to track the next key value to use.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: AngleWyrm |
last post by:
I have created a new container class, called the hat.
It provides random selection of user objects, both with and without
replacement, with non-uniform probabilities. Uniform probabilities are a
degenerate case, and can also be stored.
Here's the web-page, with complete source, illustration, and example usage.
Take a look-see, and post your critique!
http://home.comcast.net/~anglewyrm/hat.html
|
by: anonymous.user0 |
last post by:
The way I understand it, if I have an object Listener that has
registered as a listener for some event Event that's produced by an
object Emitter, as long as Emitter is still allocated Listener will
stay alive. Is this correct?
If this is correct, I've got a problem. Let's say I've got an object
Customer that has an PurchaseList (Collection) of Purchase objects.
Now, these Purchase objects were pulled from a datasource Datasource.
The...
|
by: Valeriu Catina |
last post by:
Hi,
I have an array class (the blitz library actually) which looks like this:
template<typename P_numtype, int N_rank>
class Array : public MemoryBlockReference<P_numtype>
, public ETBase<Array<P_numtype,N_rank> >
{
// ... code
};
|
by: silversurfer |
last post by:
Hello,
I am a little unsure whether this method really makes sense. The goal
is to add an element to a vector. This is the struct and method I am
using:
std::vector<Entry> models;
struct Entry{
int index;
FeatureVector* value;
|
by: Cylix |
last post by:
I have a 4row x 1col table, I would like to drop all the content of row
three.
Since Mac IE5.2 does not suppport deleteRow method,
I have also try to set the innerHTML=''; but it does not work.
How can I delete the node from DOM in other way?
Thanks.
| |
by: food4uk |
last post by:
Dear all :
I am not good at programming, please give a hand.
My data structure is very similar as an array. I actually can use the
std::vector as container to organize my data objects. However, the
behaviours of std::vector::iterator can not meet my requirements. I
need to redefine the vector::iterator's behavious such as ++. It means
"next position in the object sequence" in STL but now I redefine it to
mean "next position within a 2-d...
|
by: ademirzanetti |
last post by:
Hi there !!!
I would like to listen your opinions about inherit from a STL class
like list. For example, do you think it is a good approach if I
inherit from list to create something like "myList" as in the example
below ?
#include "Sector.h"
using namespace boost;
|
by: Peter Olcott |
last post by:
So far the only way that I found to do this was by making a
single global instance of the container class and providing
access to the contained class, through this single global
instance. Are there any other no-overhead ways that a
contained class can access its container?
The obvious choice of passing (a pointer or a reference to
the container) to the contained class is not a no-overhead
solution, it requires both memory and time. I am...
|
by: Rob McDonald |
last post by:
I am interested in having a container which has properties of both the
STL's list and vector. (I want my cake and to eat it too).
In my application, I will need to add/remove items from arbitrary
points in the container.
I will also need to be able to perform random access to elements of
the container -- accessed by index, not associatively.
Fortunately, in my application, I don't need to do both of these
|
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,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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...
|
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,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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();...
|
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...
| |
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 we have to send another system
| |