Overloading new operator

This is a discussion on Overloading new operator within the C++ Programming forums, part of the General Programming Boards category; I was trying to learn how to overload the new operator, but the code doesn´t work as it should. What ...

  1. #1
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Overloading new operator

    I was trying to learn how to overload the new operator, but the code doesn´t work as it should. What is wrong?
    Code:
    #include <iostream>
    
    using namespace std;
    
    class Example
    {
       public:
          Example(){ cout << "Constructor called"; }
          void* operator new(size_t size){ 
             return(new Example()); 
          }
    };
    
    int main()
    {
       Example *ob1;
       ob1 = new Example();
       return 0;
    }
    It doesn´t print the message.
    Thanks any help!
    Nothing more to tell about me...
    Happy day =)

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148
    Code:
    #include <cstdlib>
    ...
    class Example
    {
    public:
    	Example(){ cout << "Constructor called"; }
    	void* operator new(size_t size)
    	{ 
    		return std::malloc(size); 
    	}
    };

  3. #3
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    I think you are mistaken about the role of the new operator. In fact, the overloaded new operator is not intended to do the initialization work or even to call the constructor. It's here only for allocating memory. So, you shouldn't be using the new operator which will call the constructor.

    The second problem is that you didn't call the right operator, actually, you forgot the scope operator :: before the new keyword thus making your operator the local one so the fonction was calling itself.

    Just try return ::new Example(); and you'll notice that the constructor is called twice.

    As a matter of fact, we're missing something again; yes, we do not call the fonction an infinite time anymore but now we do the initialization twice... Because we're not calling the right operator once more. This time, we forgot the keyword operator which means that... we're using an operator. So, let us add it. With return :perator new(sizeof(Example)); we get what we expected, the object is correctly allocated, then initialized only once.

    So, the thing works pretty well, but there's no interest in calling the original new inside of your's neither is there any good in calling malloc instead of using the default new operator as actually, the default new operator just allocate memory with malloc() or maybe a native routine which is then even faster. But I think your code was for learning purpose so there's no harm in doing so.

    But why was it going fine considering it was calling itself again and again?
    I don't really know but I guess that the stack limit was reached and the program was forced to stop.

    I hope that long post was understandable and it could be of some help to you.

  4. #4
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Thanks a lot!
    The process is a little bit more complicated than I have thought, but anyway, it worked and I am happy!!
    Nothing more to tell about me...
    Happy day =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 08:05 AM
  3. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  4. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 01:55 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2003, 12:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21