+ Reply to Thread
Results 1 to 3 of 3

Thread: How to overload new operator?

  1. #1

    How to overload new operator?

    Hi All,

    I am learning C++ and I would like to overload a new operator.

    How can I do this?

  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    46
    Following example overloads new operator for user defined class.

    Code:
    class CMyAllocator
    {
    public:
    	CMyAllocator()
    	{
    	}
    
    	~CMyAllocator()
    	{
    	}
    
    	void* operator new(size_t nSize)
    	{
    		void *pTemp = malloc(nSize) ;
    		return pTemp ;
    	}
    
    	int GiveMeValue() 
    	{
    		return 42 ;
    	}
    
    } ;
    
    void UseNewoperator()
    {
         CMyAllocator* pMyAllocator = new CMyAllocator() ;
    
         int nValue = pMyAllocator->GiveMeValue() ;
    	
         delete pMyAllocator ;
    }

  3. #3
    Thank you very much for example.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts