473,695 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A container object with an index that nonetheless allows arbitrary new and delete

1 New Member
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.

Oct 16 '06 #1
1 1354
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.
Oct 16 '06 #2

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: 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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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: 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: 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: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.