Restriction on creation of object into heap, stack, static memory

Discussion in 'C++' started by asadullah.ansari, Mar 11, 2008.

  1. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    This is just for a kind of information.

    Here I am going to explain how to make a class so that user can create object
    of it on free List( heap memory) or Stack.

    Example 1: Object should be created Only On Heap memory.Idea is that make
    constructor as private so that no one create on stack. But one drawback is
    that constructor can be overloaded so better make destructor as private
    member of class because Destructor can'nt be overloaded. It's very safe
    to make destructor as private member of class for this purpose.
    Code:
    class HeapOnly
    {
     public:
     void DestroyMe() //This function will call destructor
     {
       delete this;   // Current object will be deleted means destructor
     } 
     private:
     ~HeapOnly();     // destructor only can be call by member function
    };
    
    Now you can test above class.
    Code:
    int main()
    {
     HeapOnly 
     HeapOnly * ptr = new HeapOnly;  // Object created on heap
     ptr->DestroyMe()                // Object deallocated
    }
    
    Example 2: Object should be created Only On stack memory or static memory. Idea
    is that just overload the new and delete operator and make it private member
    and dummy implemetation.

    Code:
    class autoOnly
    {
      public:
        autoOnly()
        {
        ;
        }
        ~autoOnly()
        {
        ;
        }
      private:
        void* operator new(size_t)
        {
          // Dummy Implementation
        }
        void* operator new[](size_t)
        {
          // Dummy Implementation
        }
        void operator delete(void*)
        {
           // Dummy Implementation
        }
        void operator delete[](void*)
        {
          //Dummy Implementation
        }
    };
    
    Now you can test it .
    Code:
    int main()
    {
      autoOnly *ptr= new autoOnly; // Error " cannt Access private member
      autoOnly obj;  // Created on stack
      static autoOnly obj1;  //Created on static memory
      return 0;
    }
     
  2. rashida.par

    rashida.par New Member

    Joined:
    Feb 14, 2008
    Messages:
    21
    Likes Received:
    1
    Trophy Points:
    0
    its a nice one i tried and god a lot of help
     
  3. heena.mca

    heena.mca New Member

    Joined:
    Feb 14, 2008
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
  4. rajkumar_singhalmca

    rajkumar_singhalmca New Member

    Joined:
    Aug 22, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    bikaner(raj)
    hello

    here u will knoew that how to will create that the object
    but in mode the privat member function how to will create object and how to will access them.
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice