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

  • Home
  • SEARCH
  • 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 6251917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:42:06+00:00 2026-05-24T13:42:06+00:00

Suppose I have some kind of factory function which creates objects that are largely

  • 0

Suppose I have some kind of factory function which creates objects that are largely used for a very short timespan only (possibly just for the duration of the scope of the function where this factory function is called).

Like this:

foo factory(some_parameter fancy_parameter)
{
   return foo(fancy_parameter);
}

//this gets called all the time... very often
void every_frame_function()
{
   for(int i=0; i<big_number; ++i)
       do_something_with(factory(some_parameter(i));

} //don't need those foos out here!

Is there a way to implement such factories without having the user care about memory management (by returning a pointer), without having to deal with smartpointer overhead and without returning a foo object that has to be hardcopied?

Maybe I’m asking for a goose that lays golden eggs here, but maybe there are some move semantics to be used here (I just don’t know how).

  • 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-24T13:42:08+00:00Added an answer on May 24, 2026 at 1:42 pm

    Use std::unique_ptr<T>, it has zero overhead compared with a raw pointer.

    Or simply return by value, but then you cannot do subtype polymorphism.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a method that takes an object of some kind as an
Suppose I have a Collection of some kind that itself contains Collections; e.g., Dictionary(Of
Suppose there is a business function you need to implement, which sets some kind
Suppose I have some code that would, in theory, compile against any version of
I have some kind of an ideological question, so: Suppose I have some templated
suppose I have some numbers that form a series for example: 652,328,1,254 and I
Suppose I have some XAML like this: <Window.Resources> <v:MyClass x:Key=whatever Text=foo\nbar /> </Window.Resources> Obviously
Suppose we have some named enums: enum MyEnum { FOO, BAR = 0x50 };
Suppose I have some code: let listB = [ 1; 2; 3 ] Using
Suppose I have some per-class data: (AandB.h) class A { public: static Persister* getPersister();

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.