🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #9

New/Constructor & Destructor/Delete Relationship

#9
From: Heeren Pathak
Date: Thu, 29 Oct 1992 16:18
56 lines
1615 bytes
I have a situation where I need to know how an object was created
(dynamically or on the stack) and have delete only delete dynamic objects. 
I came up with the following mechanism:

class Base {

public:
   new(...);
   Base(...);
   ~Base();
	  delete(...);

   static int createdDynamically;
		 
			int objectIsDynamic;
};

Base::createdDynamically = 0;

Base::new(...)
{
  Base::createdDynamically  = 1;
	 // do other stuff;
}

Base::Base(...)
{
  this->objectIsDynamic = Base::createdDynamically;
  Base::createdDynamically  = 0;
	 // do other stuff;
}

Base::~Base()
{
  Base::createdDynamically = this->objectIsDynamic;
  // Do other stuff
}

Base::delete(...)
{
  int isDynamic = Base::createdDynamically;
  Base::createdDynamically = 0;
  // do other stuff
}

Assuming single tasking machine.

This scheme should allow me to pass information between new and delete as
long as I am guarenteed that the constructor is called after the new.  I
haven't been able to find any reference to this in ARM so.... Does anyone
know the correct answer?


Heeren Pathak
pathak@mitre.org
Technical Details
Message-ID: <pathak-291092115943@maclab1-e.mitre.org>
Path: arc1-ca.pugleaf.net!sparky!uunet!charon.amdahl.com!pacbell.com!ames!agate!linus!linus.mitre.org!maclab1-e.mitre.org!user

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c++
Path: arc1-ca.pugleaf.net!sparky!uunet!charon.amdahl.com!pacbell.com!ames!agate!linus!linus.mitre.org!maclab1-e.mitre.org!user
From: pathak@mitre.org (Heeren Pathak)
Subject: New/Constructor & Destructor/Delete Relationship
Message-ID: <pathak-291092115943@maclab1-e.mitre.org>
Followup-To: comp.std.c++
Sender: news@linus.mitre.org (News Service)
Nntp-Posting-Host: maclab1-e.mitre.org
Organization: MITRE Corp
Date: Thu, 29 Oct 1992 16:18:51 GMT
Lines: 56