473,395 Members | 1,692 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

destructor for vector

Hi group,

I have a class called container with a vector of
pointers to some base class as member. Can
someone help me find the right destructor for
this class?

Thanks, Martijn Mulder
#include <vector.h>
#include <algorithm.h>
class base{};
class first:public base{};
class second:public base{};
class third:public base{};
class container
{
private:vector<base*>b;
public:container()
{
b.push_back(new first);
b.push_back(new second);
b.push_back(new third);
}
//?? public:virtual ~container(){for_each(b.begin(),b.end(),delete);}
};
int main(int argc,char**argv)
{
container c;
return 0;
}

Jul 19 '05 #1
20 12816
On Wed, 09 Jul 2003 01:12:04 +0200, Agent Mulder wrote:
Hi group,

I have a class called container with a vector of
pointers to some base class as member. Can
someone help me find the right destructor for
this class? [snip] //?? public:virtual ~container(){for_each(b.begin(),b.end(),delete);}

[snip]

You could create a functor which does what you need:
template <class T>
struct doDelete {
void operator()(T *target) { delete target; }
};

virtual ~container() { for_each(b.begin(), b.end(), delete<base>()); }
--
Blake Kaplan

"War doesn't determine who's right, but who's left."

Jul 19 '05 #2
"Agent Mulder" <mb*******************@home.nl> wrote in message
news:be**********@news2.tilbu1.nb.home.nl...
Hi group,

I have a class called container with a vector of
pointers to some base class as member. Can
someone help me find the right destructor for
this class?

Thanks, Martijn Mulder
#include <vector.h>
#include <algorithm.h>
class base{};
class first:public base{};
class second:public base{};
class third:public base{};
class container
{
private:vector<base*>b;
public:container()
{
b.push_back(new first);
b.push_back(new second);
b.push_back(new third);
}
//?? public:virtual ~container(){for_each(b.begin(),b.end(),delete);}
};
int main(int argc,char**argv)
{
container c;
return 0;
}


#include "boost/smart_ptr.hpp" // see www.boost.org

class container
{

public:
typedef boost::shared_ptr<base> t_pbase;
private:
vector<t_pbase>b;
public:
container()
{
b.push_back(t_pbase(new first));
b.push_back(t_pbase(new second));
b.push_back(t_pbase(new third));
}
// no destructor, copy constructor, or assignment operator required
};

A class which needs a destructor, an assignment operator, or a copy
constructor almost always needs all three. In this case just adding a
destructor to your class wouldn't be good enough -- any attempt to copy an
object of this type would eventually lead to the same pointer being deleted
twice. So you would have to add an assignment operator and a copy
constructor, remembering to check for x=x blah blah blah

I recommend you take it easy on yourself and use a reference counted smart
pointer as I have shown above. It has the copy semantics you want, manages
memory for you, and in most ways acts like a regular pointer.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 19 '05 #3
John Harrison wrote:

That's why Cy's smart pointers should be considered because they remove
the need to write the destructor, copy constructor and assignment
operator, thereby simplifying your task.

john


I have never heard of these Cy smart pointers, but they sound very useful.
How are they used? Any code snippets that could show us?
Thanks,
pete
Jul 19 '05 #4

"Blake Kaplan" <mr****@hotpop.com> wrote in message
news:pa****************************@hotpop.com...
On Wed, 09 Jul 2003 01:12:04 +0200, Agent Mulder wrote:
Hi group,

I have a class called container with a vector of
pointers to some base class as member. Can
someone help me find the right destructor for
this class?

[snip]
//?? public:virtual ~container(){for_each(b.begin(),b.end(),delete);}

[snip]

You could create a functor which does what you need:
template <class T>
struct doDelete {
void operator()(T *target) { delete target; }
};

virtual ~container() { for_each(b.begin(), b.end(), delete<base>()); }
--
Blake Kaplan

"War doesn't determine who's right, but who's left."


class delete_pointer
{
public:
template <class __p>
void operator()(const _p* p)const
{
delete p;
}
};

and

virtual ~container(){std::for_each(b.begin(), b.end() , delete_pointer());}

Here the compiler will sort out witch type you want to delete.

-Thomas Gulbrandsen
Jul 19 '05 #5

"Peter Gregory" <pe***********@durham.ac.uk> wrote in message
news:be**********@sirius.dur.ac.uk...
John Harrison wrote:

That's why Cy's smart pointers should be considered because they remove
the need to write the destructor, copy constructor and assignment
operator, thereby simplifying your task.

john


I have never heard of these Cy smart pointers, but they sound very useful.
How are they used? Any code snippets that could show us?
Thanks,
pete

#include <iostream>
#include <string>
#include <set>
#include <boost\shared_ptr.hpp>
void print_smart_ptr(const boost::shared_ptr<std::string> smart_ptr)
{
std::cout << smart_ptr->c_str() << std::endl;
}
void print_ptr(const std::string* const s)
{
std::cout << s->c_str() << std::endl;
}
int main(int argc, _TCHAR* argv[])
{
//create a shared_ptr
boost::shared_ptr<std::string> smart_ptr(new std::string("smart_ptr"));

//send it to a function
print_smart_ptr(smart_ptr);

//operator*
*smart_ptr = "smart_ptr again";
print_smart_ptr(smart_ptr);

//get the raw_pointer
std::wstring* r = smart_ptr.get();
print_ptr(r);

//reallocation
smart_ptr.reset(new std::string("smart_ptr_reset"));
print_smart_ptr(smart_ptr);

//swap smart_ptr
boost::shared_ptr<std::string> swap(new std::string("swaped"));
smart_ptr.swap(swap);
print_smart_ptr(smart_ptr);

return 0;
}

This is a litle code snippet that show some use of boost::shared_ptr

Go to this link for more info
http://www.boost.org/libs/smart_ptr/shared_ptr.htm

-Thomas Gulbrandsen
Jul 19 '05 #6

"Peter Gregory" <pe***********@durham.ac.uk> wrote in message
news:be**********@sirius.dur.ac.uk...
John Harrison wrote:

That's why Cy's smart pointers should be considered because they remove
the need to write the destructor, copy constructor and assignment
operator, thereby simplifying your task.

john


I have never heard of these Cy smart pointers, but they sound very useful.
How are they used? Any code snippets that could show us?
Thanks,
pete


I think a book would be need to explain all the possibilities. 'More
Effective C++' by Scott Meyers has a detailed section on reference counting
which is usually what people are talking about when the say smart pointer.

john
Jul 19 '05 #7


Cy Edmunds wrote:
#include "boost/smart_ptr.hpp" // see www.boost.org

When did the Boost library get added to standard C++, the only topic of
this newsgroup?


Brian Rodenborn
Jul 19 '05 #8
In article <3F***************@company.com>, Default User
<fi********@company.com> wrote:

| Cy Edmunds wrote:
|
| > #include "boost/smart_ptr.hpp" // see www.boost.org
|
|
| When did the Boost library get added to standard C++, the only topic of
| this newsgroup?
|
|
|
|
| Brian Rodenborn

Well, on April 11, 2003 the C++ standards committee voted a proposal
based on the contents of "boost/smart_ptr.hpp" into the first library
technical report. It could start appearing in your local
vendor-supplied <memory> header under namespace std::tr1 just any day
now.

That seems pretty on topic to me, especially for a newsgroup called
comp.lang.c++ (as opposed to comp.std.c++).

In fact I find Cy's post infinitely more on topic, and more helpful,
than say the following posts:

In article <3F***************@company.com>, Default User
<fi********@company.com> wrote:

| Alexander Terekhov wrote:
| >
| > Default User wrote:
| > [...]
| > > Yeah . . . I going to need you to go into my killfile. Yeah . . . move
| > > all the way to the back. Thanks, that'll be great.
| >
| > I'm just curious: how BIG is your killfile?
|
| Don't worry Al, there's a place for you if you really need it.
|
|
|
| Brian Rodenborn

In article <3F***************@company.com>, Default User
<fi********@company.com> wrote:

| Alexander Terekhov wrote:
| >
| > Default User wrote:
|
| > > Don't worry Al, there's a place for you if you really need it.
| >
| > For free?
|
|
| I didn't charge you for the cheese, did I?
|
|
|
| Brian Rodenborn

In article <3F***************@company.com>, Default User
<fi********@company.com> wrote:

| Alexander Terekhov wrote:
| >
| > Default User wrote:
|
| > > I didn't charge you for the cheese, did I?
| >
| > Cheese? I had to pay for garbage recycling.
|
|
| What?! That was the finest genetically engineered Merican cheese food
| substitute. All you had to do was unwrap it and place it on the ground,
| it would have dissolved a tunnel straight down at least 500 feet (that's
| like 29657.222 kizzometrics or whatever weird measurements you use over
| there). No disposal costs were required at all.
|
| You don't know much about cheese.
|
|
|
|
| Brian Rodenborn

In article <3F***************@company.com>, Default User
<fi********@company.com> wrote:

| "E. Robert Tisdale" wrote:
|
| > There is seldom a good reason why a legitimate post
| > to a technical newsgroup like comp.lang.c++
| > should evoke a strong emotional response in any subscriber.
| > If it does, you should suspect a troll.
|
|
| Well, I feel that way about many of your posts, especially on
| comp.lang.c.
|
|
|
|
| Brian Rodenborn

In article <3F***************@company.com>, Default User
<fi********@company.com> wrote:

| David White wrote:
| >
| > E. Robert Tisdale <E.**************@jpl.nasa.gov> wrote in message
| > news:3F**************@jpl.nasa.gov...
| > > Has anybody heard from Neil Butterworth lately?
| >
| > No, but there is this post in Google:
|
| > He's simply playing the deputy of Mr. NeilB (he's currently on
| > vacation) and Mr. "Default User" desperately wants to become a
| > deputy of Mr. v.********@attAbi.com-Please-remove-capital-A's.
|
|
| Oh, hey, I missed this the first time! If I become deputy do I get a tin
| star?
|
| It's nice that Al thinks so highly of me.
|
|
|
| Brian Rodenborn

In article <3F***************@company.com>, Default User
<fi********@company.com> wrote:

| Alexander Terekhov wrote:
|
| > Yeah. Now glide your Boeing down to earth and try to catch up on
| > the Airbus Super Jumbo.
|
|
| I don't work on that icky old commercial stuff.
|
|
|
| Brian Rodenborn

--
Howard Hinnant
Jul 19 '05 #9

Howard Hinnant wrote:
[...]
In fact I find Cy's post infinitely more on topic, and more helpful,
than say the following posts:

In article <3F***************@company.com>, Default User
<fi********@company.com> wrote:

| Alexander Terekhov wrote: .....


Default, moral: don't mess with me. Take a hit and go away to heal
your wounds. Come back and "goto moral;".

regards,
alexander.
Jul 19 '05 #10


Howard Hinnant wrote:
Well, on April 11, 2003 the C++ standards committee voted a proposal
based on the contents of "boost/smart_ptr.hpp" into the first library
technical report. It could start appearing in your local
vendor-supplied <memory> header under namespace std::tr1 just any day
now.

That seems pretty on topic to me, especially for a newsgroup called
comp.lang.c++ (as opposed to comp.std.c++).


Sounds like they're farther along than last I checked. In fact, nothing
on their website about it that I can see yet.

My point was that it isn't (or wasn't) standard/ People who propose its
use should make that clear. For instance, anyone working in defense or
other critical industries can't just throw in a third-party library.

If it (or part of it) is now standard, that does put a different spin on
things. Of course, as far as I can tell that only refers to smart
pointers, not other Boost elements. There's still the problem of getting
a standardized version of this feature (with correct headers and such).
Right now, most people are still going to have to use Boost as if it
were a third-party library.

Brian Rodenborn
Jul 19 '05 #11


Alexander Terekhov wrote:
Default, moral: don't mess with me. Take a hit and go away to heal
your wounds. Come back and "goto moral;".

Eh, what? Could you try that again in English? I haven't the faintest
idea what you are trying to say.


Brian Rodenborn
Jul 19 '05 #12
In article <3F***************@company.com>, Default User
<fi********@company.com> wrote:

| Howard Hinnant wrote:
|
| > Well, on April 11, 2003 the C++ standards committee voted a proposal
| > based on the contents of "boost/smart_ptr.hpp" into the first library
| > technical report. It could start appearing in your local
| > vendor-supplied <memory> header under namespace std::tr1 just any day
| > now.
| >
| > That seems pretty on topic to me, especially for a newsgroup called
| > comp.lang.c++ (as opposed to comp.std.c++).
|
| Sounds like they're farther along than last I checked. In fact, nothing
| on their website about it that I can see yet.

http://anubis.dkuug.dk/jtc1/sc22/wg21/

Click on: News 2003-07-03: What will be in the Library TR?

| My point was that it isn't (or wasn't) standard/ People who propose its
| use should make that clear. For instance, anyone working in defense or
| other critical industries can't just throw in a third-party library.

<sigh> When a major C++ library testing ground is off topic in
comp.lang.c++, something, somewhere has gone wrong. And I'm sorry I
picked on you. You didn't start this nonsense, you just happened to be
the latest topic-Nazi on the same day when I felt I couldn't stand it
any more. This could be a great newsgroup if there weren't so many
self appointed moderators.

If you feel something isn't on topic, just don't respond.

Having said that, if you can politely redirect someone to someplace
where he might get more information, I have no problem with that.

| If it (or part of it) is now standard, that does put a different spin on
| things.

Now that's something on-topic I can contribute: No, the library
technical report (LTR1) is not normative, meaning it is not standard.
It is informational only. Informally speaking, it basically means that
the standards committee is interested in this library. It paves the
way for including such a library in a future C++ standard. But it does
not guarantee it. And inclusion in a LTR is not a requirement for a
library to be included in a future C++ standard.

The vendors have informally agreed that libraries appearing in the
first LTR, if shipped, will go into namespace std::tr1. Though there
is no standard requirement that a vendor ship anything in a LTR.
Inclusion in this namespace will allow a smoother transition if the
library is standardized but also modified at the same time (and
presumably then migrated to namespace std).

| Of course, as far as I can tell that only refers to smart
| pointers, not other Boost elements.

See http://anubis.dkuug.dk/jtc1/sc22/wg21/ for the current list (which
may change again this October).

| There's still the problem of getting
| a standardized version of this feature (with correct headers and such).
| Right now, most people are still going to have to use Boost as if it
| were a third-party library.

<nod> Changing/modifying a standard is a very big and slow process.
Boost will play a role. Other non-boost libraries probably will too.
It is my dream that discussions concerning non-std libraries, and even
non-std C++ language extensions (typeof anybody?) could take place in
comp.lang.c++ without fear of heckling from self appointed moderators.

--
Howard Hinnant
Jul 19 '05 #13


Cy Edmunds wrote:
I'd say the moral is "don't mess with Howard." His rebuttal was a
masterpiece.

Only to a certain extent, see my rebuttal to the rebuttal. I'd have
taken a somewhat lighter tone (or not bothered) had I know the progress
of the smart pointers. However, people here have been recommending them
for quite some time where they certainly were not in any way a part of
the language.

For most people still, Boost represents a third-party library.


Brian Rodenborn
Jul 19 '05 #14


Alexander Terekhov wrote:

Default User wrote:

Alexander Terekhov wrote:
Default, moral: don't mess with me. Take a hit and go away to heal
your wounds. Come back and "goto moral;".


Eh, what? Could you try that again in English? I haven't the faintest
idea what you are trying to say.


You need to read it backwards.


moral goto and back come wounds your heal to away go and hit a take me
with mess don't moral Default.
Nope, that ain't helping, Al.

Brian Rodenborn
Jul 19 '05 #15
Default User <fi********@company.com> wrote in message news:<3F***************@company.com>...
Thomas Gulbrandsen wrote:
#include <boost\shared_ptr.hpp>

I've searched the C++ standard, and I fail to find this header. Please
don't recommend nonstandard, off-topic solutions.


It may not be in the standard, but that doesn't make it nonstandard.
boost/shared_ptr.hpp is a source file suited for inclusion (16.2/1)
containing standard C++ code (and certainly with a C++ interface).
Discussing and evaluating C++ code, from whatever source, is on
topic here.

Even if you're not allowed to use third-party solutions, boost can
be used to pick up some good ideas.

Regards,
--
Michiel Salters
Jul 19 '05 #16

Default User wrote:
[...]
You didn't start this nonsense, you just happened to be
the latest topic-Nazi on the same day when I felt I couldn't stand it
any more. This could be a great newsgroup if there weren't so many
self appointed moderators.


I'm sorry, but I can't agree with that. Newsgroup topicality is
important. If more people thought about it before posting, then
reminders/cluesticks/topic-fascism wouldn't be necessary. I know my good
buddy Al will find it hard to pass up this post!


You were defeated by Howard, my friend.

regards,
alexander.

P.S. Chaos is Good. Moderation sucks.

P.P.S. Howard, please convey my "BOO" to Abrahams.

--
http://groups.google.com/groups?thre...39C93%40web.de
(Subject: shared_ptr/weak_ptr and thread-safety)
Jul 19 '05 #17


John Harrison wrote:
For most people still, Boost represents a third-party library.
But one that is easily available and widely portable. And in addition they
seem to have the ear of the standards committee.


I'm aware of that.
I've never considered the topic of this NG to be the standard C++ language.
It is things that are of interest to all C++ programmers. Smart pointers
certainly qualify, and boost have a reasonable implementation.


That's obvious, but it's at odds with most other participants (except Al
of course).

Brian Rodenborn
Jul 19 '05 #18


Michiel Salters wrote:

Default User <fi********@company.com> wrote in message news:<3F***************@company.com>...
Thomas Gulbrandsen wrote:
#include <boost\shared_ptr.hpp>

I've searched the C++ standard, and I fail to find this header. Please
don't recommend nonstandard, off-topic solutions.


It may not be in the standard, but that doesn't make it nonstandard.


Yes, it does.
boost/shared_ptr.hpp is a source file suited for inclusion (16.2/1)
containing standard C++ code (and certainly with a C++ interface).
And I'm sure it's great. I haven't used it, for the reasons I mentioned
previously. It's not standard and it's not on our approved library list,
so I can't develop with it.
Discussing and evaluating C++ code, from whatever source, is on
topic here.
But we weren't discussing the Boost code, which would be on-topic. Boost
was recommended as a solution to a problem without qualification as to
what Boost is. Under your theory, MFC or winsock would be fine.
Even if you're not allowed to use third-party solutions, boost can
be used to pick up some good ideas.


Wouldn't disagree with that.
My point here, which seems to escape many, is that Boost seems to get a
free pass that other widely ported third-party libraries don't. I think
that when it's recommended, at least an explanation of what it is is
needed.

Brian Rodenborn
Jul 19 '05 #19


Alexander Terekhov wrote:
You were defeated by Howard, my friend.

Oh, I think not.

Brian Rodenborn
Jul 19 '05 #20

Default User wrote:
[...]
I've never considered the topic of this NG to be the standard C++ language.
It is things that are of interest to all C++ programmers. Smart pointers
certainly qualify, and boost have a reasonable implementation.


That's obvious, but it's at odds with most other participants (except Al
of course).


Default is correct, I'm biased. Disclosure: I'm an acknowledged
Booster (smallprint: currently "quarantined", though) and represent
Boost.org (FOR FREE!) at The Austin Common Standards Revision Group
(technical working group established to consider the matter of a
common revision of ISO/IEC 9945-1, ISO/IEC 9945-2, IEEE Std 1003.1,
IEEE Std 1003.2 and the appropriate parts of the Single UNIX
Specification). Several of my recent DRs against POSIX were about
a boost smart pointer. Guilty.

regards,
alexander.

P.S. Default, your place is:

comp.lang.c++.defense-or-other-critical-industries

Go away from here.
Jul 19 '05 #21

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

Similar topics

2
by: | last post by:
I have this class: --------------------------------------------- class A { string a; int instances; A(); static vector<A> all; public: static A *CreateA(...................);
3
by: Timothy Madden | last post by:
I know I can call destructors if I want (probably with their fully qualified name). However I don't see the point here. When and where would I want to call destructors ? Destructors are not...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
12
by: ypjofficial | last post by:
Hello all, I have encountered with following strange problem. I am coding in C++ and using VC++ 6 compiler. I have a class strvector containing char * cstr as a private member and i have...
8
by: gw7rib | last post by:
I've been bitten twice now by the same bug, and so I thought I would draw it to people's attention to try to save others the problems I've had. The bug arises when you copy code from a destructor...
5
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget...
4
by: David | last post by:
Hi all, something wrong with my code, it seems that the destructor function has been called twice. I don't know why.here is my codes Fixture.h #ifndef _FIXTURE_H #define _FIXTURE_H #include...
6
by: Lambda | last post by:
For a std::vector, what's the difference between the clear() function and its destructor? I know their usages are different, but how about their implementations?
9
by: itdevries | last post by:
Hi, I've ran into some trouble with an overloaded + operator, maybe someone can give me some hints what to look out for. I've got my own custom vector class, as a part of that I've overloaded...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...
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...

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.