CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: overriding new

  1. #1
    Join Date
    Jun 2005
    Posts
    7

    overriding new

    I've implemented a new operator with a parameter in one of my classes as such:

    Code:
    class test
    {
    public:
    	static void* operator new (size_t size, int somevalue)
    	{
    		return malloc(size);
    	}
    };
    However, because I'm defining a parametered new operator, the default new operator ceases to exist and I get compiler errors if I want to instance an object using the default new.

    Is there any way to keep the default new operator or do I have to redefine it manually?

  2. #2
    Join Date
    Jun 2003
    Location
    not-so-Great Britain
    Posts
    178

    Re: overriding new

    I think you either have to redefine it manually or provide default arguments for all parameters except the size_t.
    Hungarian notation is the bane of self documenting code.
    Forget all fancy tricks with operator precedence. Code should be easily readable.
    May the BOOST be with you.
    Good free E-Books thanks to Bruce Eckel.

  3. #3
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: overriding new

    Beside providing a default operator new, you also have to provide a corresponding operator delete.

    Code:
    #include <iostream>
    using namespace std;
    
    class test
    {
    public:
        test()
        {
            cout << "test::test()" << endl;
        }
        ~test()
        {
            cout << "test::~test()" << endl;
        }
        
        static void *operator new(size_t size, int value)
        {
            cout << "placement new" << endl;
            return malloc(size);
        }
    
        static void *operator new(size_t size)
        {
            cout << "operator new" << endl;
            return ::operator new(size);
        }
    
        static void operator delete(void *p, int value)
        {
            cout << "placement delete" << endl;
            free(p);
        }
    
        static void operator delete(void *p)
        {
            cout << "operator delete" << endl;
            ::operator delete(p);
        }
    };
    
    
    int main(void)
    {
    
        test *p1 = new(1) test;
        p1->~test();  // Required to directly call the destructor because placement delete never invoke it.
        test::operator delete (p1, 1);
    
        test *p2 = new test;
        delete p2;
    
        return 0;
    }

  4. #4
    Join Date
    Jun 2005
    Posts
    7

    Re: overriding new

    Yeah thanks, I was aware of that, just couldn't be bothered putting it in the example

    I guess I just have to redefine it then.

  5. #5
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: overriding new

    Have you tried test* p = ::new test; ? (Note the scope resolution operator.)
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  6. #6
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: overriding new

    Just to clarify my last answer, here's section 5.3.4/9 of the standard:
    If the new-expression begins with a unary :: operator, the allocation function’s name is looked up in the global scope. Otherwise, if the allocated type is a class type T or array thereof, the allocation function’s name is looked up in the scope of T. If this lookup fails to find the name, or if the allocated type is not a class type, the allocation function’s name is looked up in the global scope.
    (Note that, by supplying an operator new in class scope, you are hiding the global one.)
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured