473,831 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delete of objects whose pointers are stored in an array

I have these classes:

Boss,
CommissionWorke r,
PieceWorker,
HourlyWorker

all directly derived from Employeer.

I create a dynamic object (on heap) for each class:

////////////////////////////////////////////////////////////////////

Boss* b = new Boss( "Whites", "Frank", 5, 7, 1971, 1, 9, 1994, 1300);

CommissionWorke r* c = new CommissionWorke r("Reds", "Tom", 1, 2, 1980, 4,
5, 2001, 100, 1000, 2/100 );

PieceWorker* p = new PieceWorker( "Greens", "Bob", 24, 2, 1978, 4, 5,
2001, 10, 100 );

HourlyWorker* h = new HourlyWorker ("Blacks", "Mike", 12, 3, 1967, 13,
4, 2008, 20, 123 );

////////////////////////////////////////////////////////////////////
Then I "put" this pointers into a array of pointer to base class ( so to
upcast ):

////////////////////////////////////////////////////////////////////
Employeer* E[ 4 ] = { b, c, p, h };

////////////////////////////////////////////////////////////////////

After, I want to delete these objects from heap, with delete.

How I can do as simple as possible?
Is it correct following?

////////////////////////////////////////////////////////////////////

for ( int i = 0; i < 4; i++ ) {
delete *( E+i ) ;
}
Aug 7 '08 #1
6 1610
nembo kid wrote:
I have these classes:

Boss,
CommissionWorke r,
PieceWorker,
HourlyWorker

all directly derived from Employeer.

I create a dynamic object (on heap) for each class:

////////////////////////////////////////////////////////////////////

Boss* b = new Boss( "Whites", "Frank", 5, 7, 1971, 1, 9, 1994, 1300);

CommissionWorke r* c = new CommissionWorke r("Reds", "Tom", 1, 2, 1980, 4,
5, 2001, 100, 1000, 2/100 );

PieceWorker* p = new PieceWorker( "Greens", "Bob", 24, 2, 1978, 4, 5,
2001, 10, 100 );

HourlyWorker* h = new HourlyWorker ("Blacks", "Mike", 12, 3, 1967, 13,
4, 2008, 20, 123 );

////////////////////////////////////////////////////////////////////
Then I "put" this pointers into a array of pointer to base class ( so to
upcast ):

////////////////////////////////////////////////////////////////////
Employeer* E[ 4 ] = { b, c, p, h };

////////////////////////////////////////////////////////////////////

After, I want to delete these objects from heap, with delete.

How I can do as simple as possible?
Is it correct following?
Yep.
>
////////////////////////////////////////////////////////////////////

for ( int i = 0; i < 4; i++ ) {
delete *( E+i ) ;
I would write

delete E[i];

but that's actually equivalent to what you wrote, so it's a question of
style and readability, not the functionality.
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 7 '08 #2
Victor Bazarov ha scritto:

but that's actually equivalent to what you wrote, so it's a question of
style and readability, not the functionality.

Thanks.
Aug 7 '08 #3
On Aug 7, 12:26*pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
nembo kid wrote:
[snippers]
How I can do as simple as possible?
Is it correct following?

Yep.
[snips]

As long as the OP remembers this.

http://www.parashift.com/c++-faq-lit....html#faq-20.7

While the OP is there, it would not be a bad idea to
browse other parts of the FAQ.
Socks
Aug 7 '08 #4
Puppet_Sock wrote:
On Aug 7, 12:26 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
>nembo kid wrote:
[snippers]
>>How I can do as simple as possible?
Is it correct following?
Yep.
[snips]

As long as the OP remembers this.

http://www.parashift.com/c++-faq-lit....html#faq-20.7
Absolutely. As soon as pressed the 'Send' button, I thought I ought to
add that... I just assume that the parts that are not shown are correct
in cases when the program is "working", and contain unknown number of
errors in cases when the program isn't.
>
While the OP is there, it would not be a bad idea to
browse other parts of the FAQ.
Socks
Second that!

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 7 '08 #5
On Aug 7, 12:14*pm, nembo kid <nembo@kidwrote :
[snip]
After, I want to delete these objects from heap, with delete.

How I can do as simple as possible?
[snip]

"As simple as possible" is to rely on a smart pointer class (e.g.,
std::tr1::share d_ptr aka boost::shared_p tr) to do it for you. Then you
don't have to worry about deletion at all, and you are secure in the
face of premature exits from the relevant scope (e.g. return or an
exception) which could leave you with a memory leak. Indeed, it's a
good idea to use such RAII techniques (cf. http://www.research.att.com/~bs/bs_faq2.html#finally)
to manage all resources (see _C++ Coding Standards_ by Sutter and
Alexandrescu).

You could also use a smart pointer like the ones discussed in this FAQ
and following:

http://www.parashift.com/c++-faq-lit...html#faq-16.22

Or like the one presented in this chapter from _Modern C++ Design_ by
Alexandrescu:

http://www.informit.com/articles/art...seqNum=1&rll=1

whose most updated source can be found here:

http://loki-lib.sourceforge.net/

Cheers! --M
Aug 7 '08 #6
On Aug 7, 6:14*pm, nembo kid <nembo@kidwrote :
I have these classes:

Boss,
CommissionWorke r,
PieceWorker,
HourlyWorker

all directly derived from Employeer.

I create a dynamic object (on heap) for each class:

////////////////////////////////////////////////////////////////////

Boss* b = new Boss( "Whites", "Frank", 5, 7, 1971, 1, 9, 1994, 1300);

CommissionWorke r* c = new CommissionWorke r("Reds", "Tom", 1, 2, 1980, 4,
5, 2001, 100, 1000, 2/100 );

PieceWorker* p = new PieceWorker( "Greens", "Bob", 24, 2, 1978, 4, 5,
2001, 10, 100 );

HourlyWorker* h = new HourlyWorker ("Blacks", "Mike", 12, 3, 1967, 13,
4, 2008, 20, 123 );

////////////////////////////////////////////////////////////////////

Then I "put" this pointers into a array of pointer to base class ( so to
upcast ):

////////////////////////////////////////////////////////////////////

Employeer* E[ 4 ] = { b, c, p, h };

////////////////////////////////////////////////////////////////////

After, I want to delete these objects from heap, with delete.

How I can do as simple as possible?
Is it correct following?

////////////////////////////////////////////////////////////////////

for ( int i = 0; i < 4; i++ ) {
* * * *delete *( E+i ) ;

}

A little bit off topic, but I would also recommend using a higher form
of abstraction instead of C-style arrays. Just use a std::vector. It's
nearly as fast as an array, is compatible with C code requesting
arrays and it is far more readable and easy to use. Together with the
STL algorithms and the DeleteObject template function class (mentioned
in Scott Meyers Effective STL), vectors holding pointers to dynamic
allocated memory, can be nicely cleaned up like this.

std::vector<Emp loyeer*e;
e.push_back(b);
e.push_back(c);
e.push_back(p);
e.push_back(h);

std::for_each(e .begin(), e.end(), DeleteObject<Em ployeer>());

Ofcourse the deletion would be obsolete if you would store shared_ptr
objects instead of raw pointers, already mentioned by mlimber.
Aug 8 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
1573
by: Kareem Nutt | last post by:
I'm fairly new to c++ coming from c, so I'm a little confused. I have the following: Page *pageTable; Desc *descTable; pageTable = new Page; //array of Page objects descTable = new Desc; //array of Desc objects Later on I have the following for loop:
15
2397
by: Roy Smith | last post by:
I understand that "delete xp" deletes a scalar object and "delete xp" deletes an array of objects, but what I don't understand is why you need to tell the compiler which you're doing. When you do "delete xp", the delete procedure (not sure if that's the right terminology) obviously knows how many objects were allocated by the corresponding "new" call. So, why can't it just know whether you did "new x" or "new x"?
11
1794
by: DamonChong | last post by:
Hi, I am new to c++. I recently spend an enormous among of time troubleshooting a seeminly innocuous piece of code. Although I narrow down this piece of code as the culprit but I don't understand why. Can some guru help to enlighten me? Thank you. // I created an array of pointers to object pointers: Object ** obs = new Object * ;
6
2002
by: R.Z. | last post by:
i'm using a class from some api that is said to automatically call its destructor when its out of scope and deallocate memory. i create instances of this class using "new" operator. do i have to explicitly call delete on these instances when i no longer need them?
14
1810
by: Dennis | last post by:
If I have a structure like; Public Structure myStructureDef Public b() as Byte Public t as String End Structure If I pass this structure, will the values in the array b be stored on the stack or will just a pointer to the array be stored on the stack? I am trying to decide whether to use Structures or Pointers. I know that M'soft
4
3637
by: KishorAditya | last post by:
Hi All, Consider the following scenario: class Top { }; class Left: virtual public Top { }; class Right: virtual public Top { }; class Bottom: public Left, public Right {}; Many books propose that object of Bottom contains three vptrs, one for Left and Bottom, one for Right and one for Top. Now my question is why the compiler is storing superclasses' vptrs in
12
2729
by: subramanian100in | last post by:
Suppose class Base { public: virtual ~Test() { ... } // ... }; class Derived : public Base
19
10769
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is there an existing adapter? Thanks, Daniel.
23
5748
by: raylopez99 | last post by:
A quick sanity check, and I think I am correct, but just to make sure: if you have a bunch of objects that are very much like one another you can uniquely track them simply by using an ArrayList or Array, correct? An example: create the object, create an array, the stuff the object into the array. Later on, assume the object is mutable, the object changes, but you can find it, if you have enough state information to uniquely identify...
0
9793
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, 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...
0
10777
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, 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...
0
10494
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...
0
10207
tracyyun
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...
0
9317
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
7748
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
6951
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();...
2
3963
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3076
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.