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 ) ;
} 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
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.
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
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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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:
|
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"?
|
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 * ;
|
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?
|
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
| |
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
|
by: subramanian100in |
last post by:
Suppose
class Base
{
public:
virtual ~Test() { ... }
// ...
};
class Derived : public Base
|
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.
|
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...
|
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: 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: 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: 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...
|
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: 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...
| |