473,287 Members | 1,655 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,287 software developers and data experts.

Getting error while making delete operator private

Hi,

I tried to make delete operator private for my class. Strangely it is
giving me error if I compile that code in VC++.NET. But it compiles
successfully on VC++6.o. Can anybody give me inputs about it. I wanted
that on my class delete should not work. Object pointer should be
deleted using my function only which is taking care of reference count
for particular class.

Thanx in advance for your inputs.

Regards,
Premal Panchal

Aug 13 '07 #1
12 2524
"Premal" <pr***********@gmail.comwrote in message
news:11**********************@o61g2000hsh.googlegr oups.com...
Hi,

I tried to make delete operator private for my class. Strangely it is
giving me error if I compile that code in VC++.NET. But it compiles
successfully on VC++6.o. Can anybody give me inputs about it. I wanted
that on my class delete should not work. Object pointer should be
deleted using my function only which is taking care of reference count
for particular class.

Thanx in advance for your inputs.
Please show what you tried that didn't work.
Aug 13 '07 #2
On Aug 13, 3:24 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Premal" <premalpanc...@gmail.comwrote in message

news:11**********************@o61g2000hsh.googlegr oups.com...
Hi,
I tried to make delete operator private for my class. Strangely it is
giving me error if I compile that code in VC++.NET. But it compiles
successfully on VC++6.o. Can anybody give me inputs about it. I wanted
that on my class delete should not work. Object pointer should be
deleted using my function only which is taking care of reference count
for particular class.
Thanx in advance for your inputs.

Please show what you tried that didn't work.
Hi,
I tried following one:

class RefCountImpl
{
private:
//data members
protected:
void operator delete(void*);
public:
//methods
};
if i have above class implementation.Then in VC++6.0 it works. You can
allocate memory using new but you cannot delete that pointer in your
clilent code. You have to use some method provided by above class to
release the pointer.

Same thing doesnt work in VC++.NET. It clearly throws error that
making delete private cause memory leakage. May be VC++.NET compiler
become more stirct about this. :(.....

I hope you got my point. I hope you can give me some valuable input.

Regards,
Premal Panchal

Aug 13 '07 #3
Hi,
I tried following one:

class RefCountImpl
{
private:
//data members
protected:
void operator delete(void*);
public:
//methods
};
if i have above class implementation.Then in VC++6.0 it works. You can
allocate memory using new but you cannot delete that pointer in your
clilent code. You have to use some method provided by above class to
release the pointer.

Same thing doesnt work in VC++.NET. It clearly throws error that
making delete private cause memory leakage. May be VC++.NET compiler
become more stirct about this. :(.....
First off, that is not the delete operator (despite it's unfortunate
name) it is the memory deallocation function. Compilers are free to
issue whatever diagnostics they want over the required ones.

Why did you do this?
Aug 13 '07 #4
On Aug 13, 12:33 pm, Premal <premalpanc...@gmail.comwrote:
On Aug 13, 3:24 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Premal" <premalpanc...@gmail.comwrote in message
news:11**********************@o61g2000hsh.googlegr oups.com...
I tried to make delete operator private for my class. Strangely it is
giving me error if I compile that code in VC++.NET. But it compiles
successfully on VC++6.o. Can anybody give me inputs about it. I wanted
that on my class delete should not work. Object pointer should be
deleted using my function only which is taking care of reference count
for particular class.
Thanx in advance for your inputs.
Please show what you tried that didn't work.
I tried following one:
class RefCountImpl
{
private:
//data members
protected:
void operator delete(void*);
public:
//methods};
if i have above class implementation.Then in VC++6.0 it works. You can
allocate memory using new but you cannot delete that pointer in your
clilent code. You have to use some method provided by above class to
release the pointer.
Same thing doesnt work in VC++.NET. It clearly throws error that
making delete private cause memory leakage. May be VC++.NET compiler
become more stirct about this. :(.....
The problem is that the new operator must call delete if the
constructor throws. So you can't use new outside of a member
function if operator delete is private. Since this behavior was
added during standardization, some older compilers (e.g. VC++
6.0) might not implement it.

The solution, of course, is to provide factory functions as
well: static member functions which do the new.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 13 '07 #5
On Aug 13, 1:14 pm, Ron Natalie <r...@spamcop.netwrote:
I tried following one:
class RefCountImpl
{
private:
//data members
protected:
void operator delete(void*);
public:
//methods
};
if i have above class implementation.Then in VC++6.0 it works. You can
allocate memory using new but you cannot delete that pointer in your
clilent code. You have to use some method provided by above class to
release the pointer.
Same thing doesnt work in VC++.NET. It clearly throws error that
making delete private cause memory leakage. May be VC++.NET compiler
become more stirct about this. :(.....
First off, that is not the delete operator (despite it's unfortunate
name) it is the memory deallocation function.
Yes, but access control for the delete operator is done on the
memory deallocation function which is associated with it.

In this case, of course, what's bothering him is that the access
control for the new operator also checks the memory deallocation
function (because the new operator must call the memory
deallocation function if the constructor exits via an
exception).
Compilers are free to
issue whatever diagnostics they want over the required ones.
In this case, the compiler is required to issue the diagnostic.
§5.3.4:

If the new-expression creates an object or an array of
objects of class type, access and ambiguity control are
done for the allocation function, the deallocation
function (12.5), and the constructor (12.1). If the new
expression creates an array of objects of class type,
access and ambiguity control are done for the destructor
(12.4).
Why did you do this?
As he said, he didn't want any one else to delete the class.
(Think about it for a moment. For most classes that you
allocate dynamically, anything but a delete this would be an
error.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 13 '07 #6
"Premal" <pr***********@gmail.comwrote in message
news:11**********************@r34g2000hsd.googlegr oups.com...
On Aug 13, 3:24 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
>"Premal" <premalpanc...@gmail.comwrote in message

news:11**********************@o61g2000hsh.googleg roups.com...
Hi,
I tried to make delete operator private for my class. Strangely it is
giving me error if I compile that code in VC++.NET. But it compiles
successfully on VC++6.o. Can anybody give me inputs about it. I wanted
that on my class delete should not work. Object pointer should be
deleted using my function only which is taking care of reference count
for particular class.
Thanx in advance for your inputs.

Please show what you tried that didn't work.

Hi,
I tried following one:

class RefCountImpl
{
private:
//data members
protected:
void operator delete(void*);
public:
//methods
};
if i have above class implementation.Then in VC++6.0 it works. You can
allocate memory using new but you cannot delete that pointer in your
clilent code. You have to use some method provided by above class to
release the pointer.

Same thing doesnt work in VC++.NET. It clearly throws error that
making delete private cause memory leakage. May be VC++.NET compiler
become more stirct about this. :(.....

I hope you got my point. I hope you can give me some valuable input.
#include <iostream>

class RefCountImpl
{
private:
//data members
protected:
void operator delete(void*) { ::delete this };
public:
void Kill() { delete this; }
//methods};
};

int main()
{
RefCountImpl Foo;
RefCountImpl* Bar = new RefCountImpl;
Bar->Kill(); // Works
delete Bar; // Doesn't work

return 0;

}

Show how you are trying to use it. It does not fail for me unless I try to
delete the instance, are you trying to do this? Are you trying to make it
on the stack? How are you using it? Show the code that is actualy causing
the error when you compile.
Aug 13 '07 #7
On Aug 13, 8:47 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Premal" <premalpanc...@gmail.comwrote in message

news:11**********************@r34g2000hsd.googlegr oups.com...


On Aug 13, 3:24 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Premal" <premalpanc...@gmail.comwrote in message
>news:11**********************@o61g2000hsh.googleg roups.com...
Hi,
I tried to make delete operator private for my class. Strangely it is
giving me error if I compile that code in VC++.NET. But it compiles
successfully on VC++6.o. Can anybody give me inputs about it. I wanted
that on my class delete should not work. Object pointer should be
deleted using my function only which is taking care of reference count
for particular class.
Thanx in advance for your inputs.
Please show what you tried that didn't work.
Hi,
I tried following one:
class RefCountImpl
{
private:
//data members
protected:
void operator delete(void*);
public:
//methods
};
if i have above class implementation.Then in VC++6.0 it works. You can
allocate memory using new but you cannot delete that pointer in your
clilent code. You have to use some method provided by above class to
release the pointer.
Same thing doesnt work in VC++.NET. It clearly throws error that
making delete private cause memory leakage. May be VC++.NET compiler
become more stirct about this. :(.....
I hope you got my point. I hope you can give me some valuable input.

#include <iostream>

class RefCountImpl
{
private:
//data members
protected:
void operator delete(void*) { ::delete this };
public:
void Kill() { delete this; }
//methods};

};

int main()
{
RefCountImpl Foo;
RefCountImpl* Bar = new RefCountImpl;
Bar->Kill(); // Works
delete Bar; // Doesn't work

return 0;

}

Show how you are trying to use it. It does not fail for me unless I try to
delete the instance, are you trying to do this? Are you trying to make it
on the stack? How are you using it? Show the code that is actualy causing
the error when you compile.- Hide quoted text -

- Show quoted text -

Hi Jim,

Sorry for replying late. I was out of the town. I may be not available
for two days.. But just let me know whether you have tried on VC++6.0
or VC++.NET. In case of VC++6.0 above one is working. But its not
working in VC++.NET. May be as James has mentioned that according new
standard it may be the problem.

Actually I have to work on both VC++6.0 and VC++.NET. Once I reach at
my place I will post you exact error. But it is giving error at
compile time itself in VC++.NET while making delete operator private.

Really thanx to both of you JIM and JAMES. Thanx for your input. If
you find something interesting about it then let me know. As I told
you I am implementing class which is extension to auto_ptr and it
works on reference count basis. If possible I will try to post you
whole design of that.

I hope you post soon something intereting about it.

Regards,
Premal

Aug 14 '07 #8
James Kanze wrote:
>Compilers are free to
issue whatever diagnostics they want over the required ones.

In this case, the compiler is required to issue the diagnostic.
§5.3.4:

If the new-expression creates an object or an array of
objects of class type, access and ambiguity control are
done for the allocation function, the deallocation
function (12.5), and the constructor (12.1). If the new
expression creates an array of objects of class type,
access and ambiguity control are done for the destructor
(12.4).
Well, he didn't show any code that actually invoked new or the
allocation function, so it wasn't clear that such a diagnostic
was required.
Aug 14 '07 #9
On Aug 14, 4:07 pm, Ron Natalie <r...@spamcop.netwrote:
James Kanze wrote:
Compilers are free to
issue whatever diagnostics they want over the required ones.
In this case, the compiler is required to issue the diagnostic.
§5.3.4:
If the new-expression creates an object or an array of
objects of class type, access and ambiguity control are
done for the allocation function, the deallocation
function (12.5), and the constructor (12.1). If the new
expression creates an array of objects of class type,
access and ambiguity control are done for the destructor
(12.4).

Well, he didn't show any code that actually invoked new or the
allocation function, so it wasn't clear that such a diagnostic
was required.
Hi Ron,

Look at the example given by Jim. It was the compilation error in my
case. I invoke it in similar fashion, but error was that as delete
can't be private. So I wanted why it is so. It comes only in .NET not
in VC++6.0.

I hope you got my point.

Regards,
Premal Panchal

Aug 14 '07 #10
On Aug 13, 5:47 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Premal" <premalpanc...@gmail.comwrote in message
news:11**********************@r34g2000hsd.googlegr oups.com...
On Aug 13, 3:24 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Premal" <premalpanc...@gmail.comwrote in message
>news:11**********************@o61g2000hsh.googleg roups.com...
I tried to make delete operator private for my class. Strangely it is
giving me error if I compile that code in VC++.NET. But it compiles
successfully on VC++6.o. Can anybody give me inputs about it. I wanted
that on my class delete should not work. Object pointer should be
deleted using my function only which is taking care of reference count
for particular class.
Thanx in advance for your inputs.
Please show what you tried that didn't work.
I tried following one:
class RefCountImpl
{
private:
//data members
protected:
void operator delete(void*);
public:
//methods
};
if i have above class implementation.Then in VC++6.0 it works. You can
allocate memory using new but you cannot delete that pointer in your
clilent code. You have to use some method provided by above class to
release the pointer.
Same thing doesnt work in VC++.NET. It clearly throws error that
making delete private cause memory leakage. May be VC++.NET compiler
become more stirct about this. :(.....
I hope you got my point. I hope you can give me some valuable input.
#include <iostream>
class RefCountImpl
{
private:
//data members
protected:
void operator delete(void*) { ::delete this };
public:
void Kill() { delete this; }
//methods};
};
int main()
{
RefCountImpl Foo;
RefCountImpl* Bar = new RefCountImpl;
The above line shouldn't work.

The original standard wasn't 100% clear about this; if the
constructor of RefCountImpl terminates with an exception, then
the code here must call delete, but in this case, the compiler
can easily determine that it cannot terminate with an exception.
Some compilers used this information to avoid requiring that
operator delete was accessible, and others didn't. (And some
older compilers, like I think VC++ 6.0, didn't even bother with
the delete -- a constructor terminates with an exception, and
you leak memory.) I believe in fact that there was a defect
report concerning this. At any rate, the current draft
explicitly says that delete must be accessible here, even if the
compiler can determine that the constructor can't possibly
throw. (It's a logical choice, because otherwise, how much
analysis should the compiler do to determine whether the
constructor can throw or not.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 14 '07 #11
"James Kanze" <ja*********@gmail.comwrote in message
news:11**********************@r34g2000hsd.googlegr oups.com...
On Aug 13, 5:47 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Premal" <premalpanc...@gmail.comwrote in message
news:11**********************@r34g2000hsd.googlegr oups.com...
On Aug 13, 3:24 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Premal" <premalpanc...@gmail.comwrote in message
>news:11**********************@o61g2000hsh.googleg roups.com...
I tried to make delete operator private for my class. Strangely it is
giving me error if I compile that code in VC++.NET. But it compiles
successfully on VC++6.o. Can anybody give me inputs about it. I
wanted
that on my class delete should not work. Object pointer should be
deleted using my function only which is taking care of reference
count
for particular class.
Thanx in advance for your inputs.
Please show what you tried that didn't work.
I tried following one:
class RefCountImpl
{
private:
//data members
protected:
void operator delete(void*);
public:
//methods
};
if i have above class implementation.Then in VC++6.0 it works. You can
allocate memory using new but you cannot delete that pointer in your
clilent code. You have to use some method provided by above class to
release the pointer.
Same thing doesnt work in VC++.NET. It clearly throws error that
making delete private cause memory leakage. May be VC++.NET compiler
become more stirct about this. :(.....
I hope you got my point. I hope you can give me some valuable input.
#include <iostream>
class RefCountImpl
{
private:
//data members
protected:
void operator delete(void*) { ::delete this };
public:
void Kill() { delete this; }
//methods};
};
int main()
{
RefCountImpl Foo;
RefCountImpl* Bar = new RefCountImpl;
The above line shouldn't work.

The original standard wasn't 100% clear about this; if the
constructor of RefCountImpl terminates with an exception, then
the code here must call delete, but in this case, the compiler
can easily determine that it cannot terminate with an exception.
Some compilers used this information to avoid requiring that
operator delete was accessible, and others didn't. (And some
older compilers, like I think VC++ 6.0, didn't even bother with
the delete -- a constructor terminates with an exception, and
you leak memory.) I believe in fact that there was a defect
report concerning this. At any rate, the current draft
explicitly says that delete must be accessible here, even if the
compiler can determine that the constructor can't possibly
throw. (It's a logical choice, because otherwise, how much
analysis should the compiler do to determine whether the
constructor can throw or not.)

=====

Well, it compiled in Microsoft VC++ .net 2003. Perhaps it won't compile in
2005.
Aug 15 '07 #12
On Aug 15, 1:26 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"James Kanze" <james.ka...@gmail.comwrote in message

news:11**********************@r34g2000hsd.googlegr oups.com...
On Aug 13, 5:47 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:


"Premal" <premalpanc...@gmail.comwrote in message
news:11**********************@r34g2000hsd.googlegr oups.com...
On Aug 13, 3:24 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
>"Premal" <premalpanc...@gmail.comwrote in message
>>news:11**********************@o61g2000hsh.google groups.com...
I tried to make delete operator private for my class. Strangely it is
giving me error if I compile that code in VC++.NET. But it compiles
successfully on VC++6.o. Can anybody give me inputs about it. I
wanted
that on my class delete should not work. Object pointer should be
deleted using my function only which is taking care of reference
count
for particular class.
Thanx in advance for your inputs.
>Please show what you tried that didn't work.
I tried following one:
class RefCountImpl
{
private:
//data members
protected:
void operator delete(void*);
public:
//methods
};
if i have above class implementation.Then in VC++6.0 it works. You can
allocate memory using new but you cannot delete that pointer in your
clilent code. You have to use some method provided by above class to
release the pointer.
Same thing doesnt work in VC++.NET. It clearly throws error that
making delete private cause memory leakage. May be VC++.NET compiler
become more stirct about this. :(.....
I hope you got my point. I hope you can give me some valuable input.
#include <iostream>
class RefCountImpl
{
private:
//data members
protected:
void operator delete(void*) { ::delete this };
public:
void Kill() { delete this; }
//methods};
};
int main()
{
RefCountImpl Foo;
RefCountImpl* Bar = new RefCountImpl;

The above line shouldn't work.

The original standard wasn't 100% clear about this; if the
constructor of RefCountImpl terminates with an exception, then
the code here must call delete, but in this case, the compiler
can easily determine that it cannot terminate with an exception.
Some compilers used this information to avoid requiring that
operator delete was accessible, and others didn't. (And some
older compilers, like I think VC++ 6.0, didn't even bother with
the delete -- a constructor terminates with an exception, and
you leak memory.) I believe in fact that there was a defect
report concerning this. At any rate, the current draft
explicitly says that delete must be accessible here, even if the
compiler can determine that the constructor can't possibly
throw. (It's a logical choice, because otherwise, how much
analysis should the compiler do to determine whether the
constructor can throw or not.)

=====

Well, it compiled in Microsoft VC++ .net 2003. Perhaps it won't compile in
2005.- Hide quoted text -

- Show quoted text -
Yes Jim,
May by you are right. It is not getting compiled. But interesting
thing is in VC++6.0 if you make delete operator protected and just
throw exception in constructor. And in your client code under try
catch block make the array of RefImplCount. In this case if you have
thrown exception after making two objects in array then it is calling
delete and hence the destructor of the class. So its very surprising
that why it is now restricted in VC++.NET 2005.

I hope you are getting my point.I will check version of .NET at my
place also.I am not able to find your email address. If you can
provide it then I will send my code to you so may be you can give
better suggestion.

I really appreciated response you have given. Thanx a lot for this.

Regards,
Premal Panchal

Aug 16 '07 #13

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

Similar topics

5
by: xuatla | last post by:
Hi, I encountered the following compile error of c++ and hope to get your help. test2.cpp: In member function `CTest CTest::operator+=(CTest&)': test2.cpp:79: error: no match for 'operator='...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
3
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >>...
5
by: asianmuscle | last post by:
I am trying to learn RAII and some template techniques by writer a smarter pointer class to manage the resource management. Since I find that a lot of the resource management is kinda the same, I...
8
by: pagantom | last post by:
Hello. newbie here. need help. here's the text, compile errors are below. thanks class date { private: char *mo; int day, yr; public:
0
by: =?Utf-8?B?cGI2NDgxNzQ=?= | last post by:
We have been having this problem for months and always assumed it was because of our somewhat complicated setup. I have just spent almost all of today making this into a simple example that can be...
7
by: JustBeSimple | last post by:
Hi Everyone, I'm having a problem compiling useing the VS2005 .NET I need help to resolve those error, I try to create a new project it doesn't help any suggestion? I got the following errors:...
6
by: APEJMAN | last post by:
I know what I'm posting here is wired, but it's been 3 days I'm workin g on these codes, but I have no result I post the code here I dont wanne bother you, but if any one of you have time to...
4
by: Chris Peters | last post by:
Hi, I want to make the delete() operator private for my class - I'm using reference counts, so I want to force users of my class to call my function rather than being able to delete and confuse...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.