Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6168287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:39:41+00:00 2026-05-23T22:39:41+00:00

Is it possible to overload the new operator so that an object isn’t created,

  • 0

Is it possible to overload the new operator so that an object isn’t created, but instead return an existing object.

If that is possible, how could you create the objects in the first place 😀

This sounds weird I know. I’m trying to hide some details from the client. I am making a game on PS2, I’d like to have the New Foo() syntax but want a list of premade objects that can be used instead.

I don’t see to circumvent this as the new operator returns a pointer to available memory.

new Foo;

struct Foo
{
    void* operator new(std::size_t)
    {
        // return pre made obj.
    }

};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:39:42+00:00Added an answer on May 23, 2026 at 10:39 pm

    You can overload operator new, but that overloaded operator doesn’t return an object. It returns the memory for an object, and the implementation arranges for the constructor to be called.

    So you can’t do quite what you want.

    Instead, if the cost you’re trying to avoid is that of memory allocation, then your overload can assign some memory from a pre-allocated block. Obviously you’re then responsible for tracking what’s free and what isn’t, and your challenge is to do this more efficiently than the allocator that comes with the PS2 devkit. That might not be too hard – you have an unfair advantage if you’re only dealing with one class, and assuming nobody derives from it, that the size of the allocations is fixed.

    If the cost you’re trying to avoid is that of calling the constructor, then operator new doesn’t help you, but you could write a sort of wrapper:

    struct FooWrapper {
        Foo *foo;
        FooWrapper(): foo(choose_a_pre_existing_foo()) { }
        ~FooWrapper() {
            foo->reset(); // clear up anything that shouldn't be kept
            return_to_the_pool_for_reuse(foo);
        }
      private:
        FooWrapper(const FooWrapper &);
        FooWrapper &operator=(const FooWrapper &);
    };
    
    Foo *choose_a_pre_existing_foo() {
        // possibly some kind of synchronization needed if list is global
        // and program is multi-threaded.
        if list_of_foos.empty() {
            return new Foo();
        } else {
            Foo *f = list_of_foos.back();
            list_of_foos.pop_back();
            return f;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey. Is it possible to overload operator<< for primitive types? Fx lets say that
Possible Duplicate: Java operator overload In c++, we can perform the operator overloading. But
Is it possible to overload class specific new/delete that is called when arrays of
Is it possible to overload the null-coalescing operator for a class in C#? Say
Is it possible to overload the default function operator (the () operator) in C#?
Why is it not possible to overload a function just by changing the return
Possible Duplicate: What’s the right way to overload operator== for a class hierarchy? In
I know you can overload an existing operator. I want to know if it
I am trying to overload the global operator new and delete for a performance
Possible Duplicate: Any reason to overload global new and delete? In c++ you can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.