473,836 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2576
"Premal" <pr***********@ gmail.comwrote in message
news:11******** **************@ o61g2000hsh.goo glegroups.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...@rock etmail.comwrote :
"Premal" <premalpanc...@ gmail.comwrote in message

news:11******** **************@ o61g2000hsh.goo glegroups.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...@rock etmail.comwrote :
"Premal" <premalpanc...@ gmail.comwrote in message
news:11******** **************@ o61g2000hsh.goo glegroups.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 objektorientier ter Datenverarbeitu ng
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.n etwrote:
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 objektorientier ter Datenverarbeitu ng
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.goo glegroups.com.. .
On Aug 13, 3:24 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
>"Premal" <premalpanc...@ gmail.comwrote in message

news:11******* *************** @o61g2000hsh.go oglegroups.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...@rock etmail.comwrote :
"Premal" <premalpanc...@ gmail.comwrote in message

news:11******** **************@ r34g2000hsd.goo glegroups.com.. .


On Aug 13, 3:24 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
"Premal" <premalpanc...@ gmail.comwrote in message
>news:11******* *************** @o61g2000hsh.go oglegroups.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.n etwrote:
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

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

Similar topics

5
2158
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=' in '*this = CTest::operator+(CTest&)((+t2))' test2.cpp:49: error: candidates are: CTest CTest::operator=(CTest&) make: *** Error 1
6
4768
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 appreciated. Thanks in advance
3
4661
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 >> 0x804d008 _Delete unknown block >> registered : 0x804d138 >> 0x804d008 _Delete unknown block >> 0x804d098 _Delete ok
5
2038
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 try to mimic some other examples outther then tailor to my needs. My case is for windows handle like hkey, dll library, file handle, etc. But the code largely uses standard C++. Only the Clean up action, I put windows api call in there. In...
8
392
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
1757
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 easily reproduced and it seems like a major .NET flaw/limitation that I was hoping you could explain to me as it is really frustrating me. I just want to be able to get info for a row clicked, whether it is a Repeater, GridView, etc. I have...
7
3113
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: vector_main.obj : error LNK2019: unresolved external symbol "public: __thiscall Vector<int>::~Vector<int>(void)" (??1?$Vector@H@@QAE@XZ) referenced in function _main
6
3269
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 read my program I appriciate it. the program suppose to print a message on the screen #include <iostream> #include <string>
4
274
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 the reference counter. But allocating memory with new() is OK. On VC++6 this works fine, but on VC++.NET it generates an error that making delete private causes memory leakage.
0
10846
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, 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...
0
10551
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 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...
1
10594
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9376
agi2029
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...
0
6979
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();...
0
5650
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5828
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4020
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3116
bsmnconsultancy
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...

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.