473,666 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

avoid destructor

I have this class:
---------------------------------------------
class A {
string a;
int instances;
A();
static vector<A> all;

public:
static A *CreateA(...... .............);

void operator delete(void *tex) {
A *t = (A*) tex;
if (t->instances) t->instances--;
}

static void GarbageCollecto r();
};
----------------------------------------------

CreateA checks if "string a" exists in any of vector's objects. If exists
increases counter and return Pointer else creates and adds in vector a new
object.
When I run "delete(objectA );" then C++ runs first destructor and after
"delete".
This means that "string a" destroyed.
How can I avoid this? Because in this pointer there are many objects and
"string a" destroyed for all. (avoid to run destructor, but run only
"delete")

Thanks
Jul 22 '05 #1
2 1780
<- Chameleon -> wrote:
I have this class:
---------------------------------------------
class A {
string a;
int instances;
A();
static vector<A> all;

public:
static A *CreateA(...... .............);

void operator delete(void *tex) {
A *t = (A*) tex;
if (t->instances) t->instances--;
}

static void GarbageCollecto r();
};
----------------------------------------------

CreateA checks if "string a" exists in any of vector's objects. If exists
increases counter and return Pointer else creates and adds in vector a new
object.
When I run "delete(objectA );" then C++ runs first destructor and after
"delete".
This means that "string a" destroyed.
How can I avoid this? Because in this pointer there are many objects and
"string a" destroyed for all. (avoid to run destructor, but run only
"delete")


Don't call it "delete", don't make it an operator, but instead
make it a function and call it "destroy". Define the operator
as private so nobody can invoke it without your knowledge.

Victor
Jul 22 '05 #2
On Tue, 27 Jul 2004 22:34:16 +0300, "<- Chameleon ->"
<ch******@hotma il.NOSPAM.com> wrote:
I have this class:
---------------------------------------------
class A {
string a;
int instances;
A();
static vector<A> all;

public:
static A *CreateA(...... .............);

void operator delete(void *tex) {
A *t = (A*) tex;
if (t->instances) t->instances--;
By the time you get to operator delete the lifetime of the object has
ended, and accessing the instances member has undefined behaviour.
}

static void GarbageCollecto r();
};
----------------------------------------------

CreateA checks if "string a" exists in any of vector's objects. If exists
increases counter and return Pointer else creates and adds in vector a new
object.
When I run "delete(objectA );" then C++ runs first destructor and after
"delete".
This means that "string a" destroyed.
How can I avoid this? Because in this pointer there are many objects and
"string a" destroyed for all. (avoid to run destructor, but run only
"delete")


Don't call delete on pointers returned by CreateA, instead add a
DestroyA static member to A and have an unimplemented private operator
delete to ensure that no-one does call delete by mistake.

A better strategy would be to use a smart pointer. e.g.

#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
using boost::shared_p tr;
using boost::weak_ptr ;

class A
{
string a;
static std::vector<wea k_ptr<A> > all;
public:
static shared_ptr<A> CreateA(..);
//...
};

See www.boost.org

Tom
Jul 22 '05 #3

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

Similar topics

52
26996
by: Newsnet Customer | last post by:
Hi, Statement 1: "A dynamically created local object will call it's destructor method when it goes out of scope when a procedure returms" Agree. Statement 2: "A dynamically created object will call it's destructor when it is made a target of a delete".
11
10509
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor" is called in Case 2 since only ~base() becomes "virtual" and ~Derived() is still non-virtual? 3. Does Case 3 show that we don't need any virtual destructor to make ~Derived() called? 4. Is "virtual destructor" needed only for Case 2?
30
3551
by: seesaw | last post by:
Is it right thing to always avoid using "new" to create objects? What if after starting the application, then decide which and how many objects to create? (Seems like under such situation is there no other choice but using "new")
6
2096
by: | last post by:
I have a class with overloading operator new. (Because, if an identical object exists, return a pointer to existed object instead of a new pointer) It has no sense (it is dangerous) to allocate an object of that class in stack. So, is there a trick to avoid stack creation of an object of this class? I mean, to take compile-time error instead of runtime. I want to create this class object only in heap. Thanks.
35
3307
by: Peter Oliphant | last post by:
I'm programming in VS C++.NET 2005 using cli:/pure syntax. In my code I have a class derived from Form that creates an instance of one of my custom classes via gcnew and stores the pointer in a member. However, I set a breakpoint at the destructor of this instance's class and it was never called!!! I can see how it might not get called at a deterministic time. But NEVER? So, I guess I need to know the rules about destructors. I would...
11
7389
by: PengYu.UT | last post by:
The following program calls the normal constructor and the copy constructor. By calling the copy constuctor is redundandant, all I want is only a vector of a trial object. Is there any way to avoid the use of the copy constructor? #include <vector> #include <iostream>
11
1979
by: AB | last post by:
Hi All, I've got an array of objects, during the execution of the program I'd like to assign a particular object to a certain element in the object array. The sample code's like this... class ClassA { public: ClassA()
8
2047
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 to use elsewhere. For example, suppose you have a class Note. This class stores some text, as a linked list of lines of text. The destructor runs as follows: Note::~Note() {
7
1995
by: sam | last post by:
Hi, See when i reading a sourcecode of a program, I read that the constructor is ordinary and after that the programmer has written virtual destructor for that constructor . Why we use the virtual destructor whats the use of it? the code is like this: Network(int input,int output); Network(&Network); virtual ~Network();
0
8444
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8781
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
8551
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
8639
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5664
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
4198
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
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1775
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.