   determine if object was created with new
  I wan't to know if my object is created with new or by default. My idea was to overload the new operator and there allocate memory for the object and then set a bool flag in my class to true. However when I don't use new, the flag is undefined as in my new operator the flag is set before the constructor is called, so setting it to false in the constructor would overwrite the true information i got from the new operator...Any ideas?some code   class A{  private:bool newFlag;  public: A() {   // this will overwrite the information set in the new    // operator, however leaving it alone will cause it to   // be undefined and has a chance being true...   newFlag = false; } void* operator new(size_t size) {    // i am uncertain if this is the right way of allocating    // memory for my class..    A* a = (A*)malloc(size);    a->newFlag = true;    return a; }};[edited by - pag on August 4, 2003 8:01:40 AM]
  why don''t you always use new? that would save you a lot of troubles. Are you deleting your object from another class? that''s dangerous and you should try to avoid doing it. ;  This class is supposed to be my base class in a library im developing, which eventually will become public for everyone, however not that anyone might use it.. Anyways to me it seems kind of risky hoping that everyone will use new to create their objects, and if they don''t their apps will crash when trying to delete something that wasn''t created with new, which will cause annoyance.  ;  The way I did it is as follows:Firstly, I have a static variable in the class which indicates whether I am currently creating an object via new.In the call to new, I set the flag.In the constructor, I copy the static flag into the instance, and then reset the static flag.Now the static flag will be set whenever we enter the constructor via new, and unset whenever we enter the constructor any other way.Observe that the behaviour is unpredictable if the class you''re instantiating contains instances of other classes, but pointers-to-instances are ok.You should use only new to allocate instances on the heap. Your implementation of delete is wanky if it crashes when passed an object that wasn''t created with new.   CoV;  doh, why didnt i think of static variables, doh...anyways, im still uncertain if im doing the right thing in my new operator... is there no way I can call the default new operator which is used by default?I think I have tried this:void* operator new(size_t size){  return ::new A();}    and if I remember correctly, it calls the constructor twice then which is not good...   quote:You should use only new to allocate instances on the heap. Your implementation of delete is wanky if it crashes when passed an object that wasn't created with new. From what I know, the only difference between malloc and new is that new calls the constructor. And the constructor is called after my new operator... so I can't see any problems with that.. However im uncertain of this and this is why im asking the above...[edited by - pag on August 4, 2003 8:20:26 AM] [edited by - pag on August 4, 2003 8:26:05 AM];  void * operator new( size_t size ){  return allocatememorybutcertainlynotcallnewagain( size );};  return ::new char[sizeof(A)];Since all you need is a block of memory with proper size. ;  sounds like you are trying to make some sort of garbage collection - if you are it would be alot easier to have a garbage collecter singleton that keeps pointers to all objects that were created with new... then you would have all info you would need in one place.Also with the above scheme if you overload the delete operator you can just silently ignore any calls on objects not created with new... /Please excuse my bad spelling - My native language is binary not english|Visit me\Take my advice - I don''''t use it... ;  If it really matters that your object has been created with new, then perhaps you might consider making the constructor private, and have a public static method to create a new object using new.  class A{  public:static void CreateA ( A*& recievePtr, const std::string& name );  private:A ( const std::string& name );}; Otherwise you might want to reconsider your design. Personally I think overloading new/delete or using a garbage collection singleton is just unnecessary complexity in this situation. Just my opinion.   [edited by - Oxyacetylene on August 4, 2003 12:19:04 PM]