Containment classes

  • Thread starter Chandrashekara Adiga
  • Start date
C

Chandrashekara Adiga

hi

This is particular to Containment classes .. ( just taking an
example from rougewave )

Can someone point out How destrutor of new RWDate(2, "Nov", 1980)
will be called?
Is is our responsibilty to call it or such container after deleting
the list element calls the destuctor also .


Will appreciate any pointer in this regard ..



RWTPtrHashDictionary<RWCString, RWDate>
birthdays.insertKeyAndValue (new RWCString("Ivan"), new RWDate(2,
"Nov", 1980) );



Thanks
CAdiga


#include <rw/tphdict.h>
#include <rw/cstring.h>
#include <rw/rwdate.h>
#include <rw/rstream.h>

main() {
RWTPtrHashDictionary<RWCString, RWDate>
birthdays(RWCString::hash);
birthdays.insertKeyAndValue
(new RWCString("John"),
new RWDate(12, "April", 1975)
);
birthdays.insertKeyAndValue
(new RWCString("Ivan"),
new RWDate(2, "Nov", 1980)
);

// Alternative syntax:
birthdays[new RWCString("Susan")] =
new RWDate(30, "June", 1955);
birthdays[new RWCString("Gene")] =
new RWDate(5, "Jan", 1981);

// Print a birthday:
RWCString key("John");
cout << *birthdays[&key] << endl;

birthdays.clearAndDestroy();
return 0;
}
 
V

Victor Bazarov

Chandrashekara Adiga said:
This is particular to Containment classes .. ( just taking an
example from rougewave )

Can someone point out How destrutor of new RWDate(2, "Nov", 1980)
will be called?

It is impossible to tell without seeing what ".clearAndDestroy()"
call does to the object ('birthdays').
Is is our responsibilty to call it or such container after deleting
the list element calls the destuctor also .

If I understand what you're asking, then it is _possible_ that
"RWTPtrHashDictionary" template is implemented so that it calls
'delete' on all of its pointers. However, since it's not a standard
type, how can we tell? If you have the documentation, read it.
Will appreciate any pointer in this regard ..

Here is a hint: in most cases if it is your code that calls 'new'
to create something, it's _your_ code that should call 'delete'
to destroy that something.
RWTPtrHashDictionary<RWCString, RWDate>
birthdays.insertKeyAndValue (new RWCString("Ivan"), new RWDate(2,
"Nov", 1980) );



Thanks
CAdiga


#include <rw/tphdict.h>
#include <rw/cstring.h>
#include <rw/rwdate.h>
#include <rw/rstream.h>

main() {
RWTPtrHashDictionary<RWCString, RWDate>
birthdays(RWCString::hash);
birthdays.insertKeyAndValue
(new RWCString("John"),
new RWDate(12, "April", 1975)
);
birthdays.insertKeyAndValue
(new RWCString("Ivan"),
new RWDate(2, "Nov", 1980)
);

// Alternative syntax:
birthdays[new RWCString("Susan")] =
new RWDate(30, "June", 1955);
birthdays[new RWCString("Gene")] =
new RWDate(5, "Jan", 1981);

// Print a birthday:
RWCString key("John");
cout << *birthdays[&key] << endl;

birthdays.clearAndDestroy();
return 0;
}

Victor
 
R

Rolf Magnus

Chandrashekara said:
hi

This is particular to Containment classes .. ( just taking an
example from rougewave )

Can someone point out How destrutor of new RWDate(2, "Nov", 1980)
will be called?

It will be called when you delete the object.
Is is our responsibilty to call it or such container after
deleting the list element calls the destuctor also .

I'm not quite sure I understand the above sentence, but if you destroy a
pointer, the object it points to doesn't get destroyed, so if you
dynamically allocated it, you must delete it, too.
RWTPtrHashDictionary<RWCString, RWDate>
birthdays.insertKeyAndValue (new RWCString("Ivan"), new RWDate(2,
"Nov", 1980) );

Do you really need all those pointers and that dynamic allocation? Looks
unneccesary to me. If you don't use dynamic allocation with new, you
don't need to care about deleting the object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top