"James Kanze" <james.ka...@gmail.com> wrote in message
news:1187093254.563437.306220@r34g2000hsd.googlegroups.com...
On Aug 13, 5:47 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:
"Premal" <premalpanc...@gmail.com> wrote in message
news:1187001196.332686.142080@r34g2000hsd.googlegroups.com...
On Aug 13, 3:24 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:
"Premal" <premalpanc...@gmail.com> wrote in message
news:1186998665.724715.167860@o61g2000hsh.googlegroups.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 -
May by you are right. It is not getting compiled. But interesting
throw exception in constructor. And in your client code under try
catch block make the array of RefImplCount. In this case if you have
delete and hence the destructor of the class. So its very surprising
that why it is now restricted in VC++.NET 2005.
place also.I am not able to find your email address. If you can
better suggestion.
I really appreciated response you have given. Thanx a lot for this.