473,467 Members | 1,981 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Delete of objects whose pointers are stored in an array

I have these classes:

Boss,
CommissionWorker,
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);

CommissionWorker* c = new CommissionWorker("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 1589
nembo kid wrote:
I have these classes:

Boss,
CommissionWorker,
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);

CommissionWorker* c = new CommissionWorker("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...@comAcast.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...@comAcast.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::shared_ptr aka boost::shared_ptr) 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,
CommissionWorker,
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);

CommissionWorker* c = new CommissionWorker("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<Employeer*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<Employeer>());

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
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;...
15
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...
11
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...
6
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...
14
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...
4
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...
12
by: subramanian100in | last post by:
Suppose class Base { public: virtual ~Test() { ... } // ... }; class Derived : public Base
19
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...
23
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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.