By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,603 Members | 1,477 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,603 IT Pros & Developers. It's quick & easy.

is 'operator delete' a function?

P: n/a
On comp.lang.c++.moderated

http://groups.google.co.uk/group/com...11da7760?hl=en

the following question was posted

-------------------------------------------------------------------------------------------------------
Is this standard conformant to type cast ::opearator delete to a
function of type (void (*)(void*));
Is not operator delete already a function of this type?
Is the following code conformant?
class A { /* ... */ };
vector<A*v;
v.push_back(new A());
// ...
for_each(v.begin();v.end();(void (*)(void*) ::operator delete);

-------------------------------------------------------------------------------------------------------

Is the following a reasonable solution .
#include<iostream>
#include<memory>
#include<vector>

using namespace std;

struct Base {
virtual ~Base(){}
};

class Derived :public Base {
public:
Derived(){
Count++;
}
Derived(const Derived&){
Count++;
}

~Derived(){Count--;}
static unsigned long Count;
};

unsigned long Derived::Count(0);

template<class T>
struct auto_pimpl {
auto_pimpl(){}
auto_pimpl(T* ptr):theInnerPtr(ptr){}
auto_pimpl(const auto_pimpl<T& theOther):
theInnerPtr(theOther.theInnerPtr.release()){};

auto_pimpl & operator=(const auto_pimpl &theRHS) {
theInnerPtr.reset();
theInnerPtr = auto_ptr<T>(theRHS.theInnerPtr.release());
return *this;
}

T& operator*(void)const{return *theInnerPtr.get();}
T* operator->()const{return theInnerPtr.get();}

private:
mutable auto_ptr<TtheInnerPtr;
};
int main() {

cout << "There are " << Derived::Count
<< " Derived Objects when we start\n";
{
vector<auto_pimpl<Base theVector;
for (unsigned long i(0);i<10;++i){
theVector.push_back(new Derived);

}
cout << "Now there are " << Derived::Count
<< " Derived Objects \n";
}

cout << "Finaly there are " << Derived::Count
<< " Derived Objects \n";
}

I am not subscribed there yet .. so couldn't post there.

Nov 13 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
* Nindi:
On comp.lang.c++.moderated
the following question was posted
[...]
I am not subscribed there yet .. so couldn't post there.
You do not have to "subscribe" to Usenet groups in order to post in
them, although particular client software may make it seem that way.

However, currently, as of 13th November 2006, since sometime yesterday,
the clc++m moderation server is down, which means that postings will be
delayed or worse. Depending on the mail server used to post, how long
this problem persists, and what the problem really is, postings to
clc++m may simply disappear.

So, wait until you see the first new posting appear in clc++m, then try
to post an on-topic message there, e.g. via Google groups.
Hope this helps,

- Alf (moderator clc++m, but a /long/ way away from that server!)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 13 '06 #2

P: n/a
Nindi wrote:
On comp.lang.c++.moderated

http://groups.google.co.uk/group/com...11da7760?hl=en

the following question was posted

-------------------------------------------------------------------------------------------------------
Is this standard conformant to type cast ::opearator delete to a
function of type (void (*)(void*));
Is not operator delete already a function of this type?
Is the following code conformant?
class A { /* ... */ };
vector<A*v;
v.push_back(new A());
// ...
for_each(v.begin();v.end();(void (*)(void*) ::operator delete);
It is only memory freeing. Destructors of objects doesn't invoke.
Try
class delete_object
{
template <typename T>
void operator()(T* p)
{
delete p;
}
};
....
for_each(v.begin(),v.end(),delete_object<T>);
-------------------------------------------------------------------------------------------------------

Is the following a reasonable solution .
#include<iostream>
#include<memory>
#include<vector>

using namespace std;

struct Base {
virtual ~Base(){}
};

class Derived :public Base {
public:
Derived(){
Count++;
}
Derived(const Derived&){
Count++;
}

~Derived(){Count--;}
static unsigned long Count;
};

unsigned long Derived::Count(0);

template<class T>
struct auto_pimpl {
auto_pimpl(){}
auto_pimpl(T* ptr):theInnerPtr(ptr){}
auto_pimpl(const auto_pimpl<T& theOther):
theInnerPtr(theOther.theInnerPtr.release()){};

auto_pimpl & operator=(const auto_pimpl &theRHS) {
theInnerPtr.reset();
theInnerPtr = auto_ptr<T>(theRHS.theInnerPtr.release());
return *this;
}

T& operator*(void)const{return *theInnerPtr.get();}
T* operator->()const{return theInnerPtr.get();}

private:
mutable auto_ptr<TtheInnerPtr;
};
int main() {

cout << "There are " << Derived::Count
<< " Derived Objects when we start\n";
{
vector<auto_pimpl<Base theVector;
for (unsigned long i(0);i<10;++i){
theVector.push_back(new Derived);

}
cout << "Now there are " << Derived::Count
<< " Derived Objects \n";
}

cout << "Finaly there are " << Derived::Count
<< " Derived Objects \n";
}

I am not subscribed there yet .. so couldn't post there.
Nov 13 '06 #3

P: n/a

Shelyag Yuriy wrote:
Nindi wrote:
On comp.lang.c++.moderated

http://groups.google.co.uk/group/com...11da7760?hl=en

the following question was posted

-------------------------------------------------------------------------------------------------------
Is this standard conformant to type cast ::opearator delete to a
function of type (void (*)(void*));
Is not operator delete already a function of this type?
Is the following code conformant?
class A { /* ... */ };
vector<A*v;
v.push_back(new A());
// ...
for_each(v.begin();v.end();(void (*)(void*) ::operator delete);
It is only memory freeing. Destructors of objects doesn't invoke.
Try
class delete_object
{
template <typename T>
void operator()(T* p)
{
delete p;
}
};
...
for_each(v.begin(),v.end(),delete_object<T>);
-------------------------------------------------------------------------------------------------------

Is the following a reasonable solution .
#include<iostream>
#include<memory>
#include<vector>

using namespace std;

struct Base {
virtual ~Base(){}
};

class Derived :public Base {
public:
Derived(){
Count++;
}
Derived(const Derived&){
Count++;
}

~Derived(){Count--;}
static unsigned long Count;
};

unsigned long Derived::Count(0);

template<class T>
struct auto_pimpl {
auto_pimpl(){}
auto_pimpl(T* ptr):theInnerPtr(ptr){}
auto_pimpl(const auto_pimpl<T& theOther):
theInnerPtr(theOther.theInnerPtr.release()){};

auto_pimpl & operator=(const auto_pimpl &theRHS) {
theInnerPtr.reset();
theInnerPtr = auto_ptr<T>(theRHS.theInnerPtr.release());
return *this;
}

T& operator*(void)const{return *theInnerPtr.get();}
T* operator->()const{return theInnerPtr.get();}

private:
mutable auto_ptr<TtheInnerPtr;
};
int main() {

cout << "There are " << Derived::Count
<< " Derived Objects when we start\n";
{
vector<auto_pimpl<Base theVector;
for (unsigned long i(0);i<10;++i){
theVector.push_back(new Derived);

}
cout << "Now there are " << Derived::Count
<< " Derived Objects \n";
}

cout << "Finaly there are " << Derived::Count
<< " Derived Objects \n";
}

I am not subscribed there yet .. so couldn't post there.

But what if there is a throw between populating the vector and the
for_each statement.. surly it is better to use a smart pointer .

Nov 13 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.