Thread: Calling global operator new inside overloaded operator new

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    1

    Calling global operator new inside overloaded operator new

    I've been asked to implement a design spec that has a curious proviso on it: an overload on operator new as a member in one of the classes. The spec requires that the overloaded new call the global new -- rather than just using malloc() -- and be easily able to have extra statements to work on the new object inserted before returning.

    It seems to me that the design is aimed at producing objects in one class hierarchy (but not the others) that are aware of whether they're on the heap or the stack (since automatic local variables shouldn't initialise via new), while still allowing the regular operator delete to clean them up, without having to overload it.

    The reason for the malloc()/free() aversion wasn't addressed when I asked about it. All the examples of overloading new I've been able to find use malloc(), with free() in an overloaded delete.

    After reading through VC++'s documentation regarding overloading new, I can't quite see how to do this, since new/delete are overloads I've never had to write before. new returns void*, but casting it straight to the type the overload is a member of should be safe, yes? Global new should be reachable by ::new, but what parameters need to be passed, and what would the call look like?

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    maybe you could use placement new instead of malloc?

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Here's what I played with:
    Code:
    #include <iostream>
    #include <new>
    #include <string>
    
    class foo
    {
    public:
    	foo() {}
    	void* operator new(size_t size, const std::string & s)
    	{
    		foo* temp = ::new foo();
    		std::cout << "New foo created, with memory managed as indicated by: " << s << std::endl;
    		return temp;
    	}
    };
    
    int main()
    {
    	foo bar;
    	foo* dyn = new("Hey there") foo();
    	std::cin.get();
           //Deal with deletion.
    }
    You'll need a matching delete operator, for exception safety.
    Last edited by CodeMonkey; 12-09-2008 at 02:14 AM. Reason: delete your junk!
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Placement new is not necessary.
    Code:
    #include <new>
    
    class foo
    {
    public:
    	foo() {}
    	void* operator new(size_t size)
    	{
    		return ::operator new(size);   // call global operator new
    	};
    };
    
    int main()
    {
    	foo bar;
    	foo* dyn = new foo;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling virtuals inside constructors
    By cloudy in forum C++ Programming
    Replies: 10
    Last Post: 05-07-2009, 04:52 AM
  2. Calling other objects methods inside a constructor
    By mynickmynick in forum C++ Programming
    Replies: 5
    Last Post: 09-17-2008, 06:31 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. calling a function inside main()
    By mero24 in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2005, 01:22 AM
  5. calling a member function inside a vector
    By finnepower in forum C++ Programming
    Replies: 2
    Last Post: 05-16-2004, 02:44 AM