CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    A question regarding operator new

    Here is the code,
    Code:
    class A
    {
    public:
    	A(int x) {}
    	static void* operator new(size_t size){}
    };
    
    int main()
    {
    	void* rawMemory = operator new[](10*sizeof(A));
    
    	A* pA = static_cast<A*>(rawMemory);
    
    	for(int i=0;i<10;i++)
    		new(&pA[i]) A(i);
    
    	for(int j=9;j>=0;j--)
    		pA[j].~A();
    
    	operator delete[](rawMemory);
    	return 0;
    }
    There is compiler error "error C2660: 'A:perator new' : function does not take 2 arguments". What does it mean? Also one more question here. What if overloaded operator new is not static? Is there any problem with operator new not being static or not being global? Thanks.

  2. #2
    Join Date
    Jul 2007
    Posts
    249

    Re: A question regarding operator new

    Change the code like this

    Code:
    #include<iostream>
    using namespace std;
    
    class A
    {
            public:
                    A(int x) {}
                    static void* operator new[](size_t size){}
    };
    
    int main()
    {
            void* rawMemory = operator new[](10*sizeof(A));
    
            A* pA = static_cast<A*>(rawMemory);
    
            for(int i=0;i<10;i++)
                    new(&pA[i]) A(i);
    
            for(int j=9;j>=0;j--)
                    pA[j].~A();
    
            operator delete[](rawMemory);
            return 0;
    }

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: A question regarding operator new

    In all my years of programming, so far I have never needed to overload operator new.

    You might be better off focusing your studies elsewhere.

  4. #4
    Join Date
    Aug 2007
    Posts
    858

    Re: A question regarding operator new

    2 things:

    I believe you need to #include <memory> in order to use placement new.
    Also, placement new should take a void*, not an A*.

    That said, I agree with Lindley.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: A question regarding operator new

    Quote Originally Posted by Speedo
    I believe you need to #include <memory> in order to use placement new.
    That would be #include <new>.

    Quote Originally Posted by Speedo
    Also, placement new should take a void*, not an A*.
    Pointers to objects are convertible to void*, so that is not a problem.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Jul 2005
    Posts
    1,030

    Re: A question regarding operator new

    I truly respect what you believe but I have different philosophy. Sometimes, knowing one thing is probably not beneficial at all. But during the process of knowing one thing, you might know many other things. Someone was trying to invent a satellite but it turns out he invented a IPhone(I made it up but it says all I want to say -- knowledge has no boundary).
    Quote Originally Posted by Lindley View Post
    In all my years of programming, so far I have never needed to overload operator new.

    You might be better off focusing your studies elsewhere.

  7. #7
    Join Date
    Jul 2005
    Posts
    1,030

    Re: A question regarding operator new

    I don't even include <new> in order to use placement new. If I don't overload operator new, then it compiles fine. Any idea why overloaded operator new causes the compiler error? Thanks.
    Quote Originally Posted by laserlight View Post
    That would be #include <new>.


    Pointers to objects are convertible to void*, so that is not a problem.

  8. #8
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: A question regarding operator new

    Quote Originally Posted by LarryChen View Post
    I don't even include <new> in order to use placement new. If I don't overload operator new, then it compiles fine. Any idea why overloaded operator new causes the compiler error? Thanks.
    maybe because it's not implemented? How do you expect to allocate anything if your new does nothing?

    at the very least, make it return null. at best, have it allocate some, and return the pointer to the allocated space.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  9. #9
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: A question regarding operator new

    Never mind, this is the problem:

    Code:
    new(&pA[i]) A(i);
    the "new operator" calls "operator new", and in particular, "new (...) X" calls "operator new(X, ...)"

    The problem is that by providing "operator nrw(size_t)", you are hiding all other operator new that are global, but could be applied to your class. Your compiler is looking for "operator new(size_t size, void* ptr)", but all it finds is "size_t size)". Remember that (Koenig) lookup stops in the first scope that has a name match. From there it tries to find a correct match from all current name match. If this fails, you don't go looking in the parent scope, you error.

    You need to add the two other operator new: The inplace one, and the nothrow one:

    Code:
    class A
    {
        public:
            A(int x) {}
            static void* operator new[](size_t size) throw (std::bad_alloc)
            {
                return ::operator new(size);
            }
            
            static void* operator new[](size_t size, const std::nothrow_t& nothrow_constant) throw()
            {
                return ::operator new(size, nothrow_constant);
            }
            
            static void* operator new[](size_t size, void* ptr) throw()
            {
                return ::operator new(size, ptr);
                //return ptr
            }
    };
    Notice though that "inplace operator new" doesn't actually do anything, but return the input pointer. This is because it doesn't have anything to allocate, and it's not its job to construct anything (that would be "new operator"'s job).
    Last edited by monarch_dodra; July 30th, 2010 at 01:50 AM.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

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