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 332581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:52:48+00:00 2026-05-12T09:52:48+00:00

Suppose you have the following code: int main(int argc, char** argv) { Foo f;

  • 0

Suppose you have the following code:

int main(int argc, char** argv) {
    Foo f;
    while (true) {
        f.doSomething();
    }
}

Which of the following two implementations of Foo are preferred?

Solution 1:

class Foo {
    private:
        void doIt(Bar& data);
    public:
        void doSomething() {
            Bar _data;
            doIt(_data);
        }
};

Solution 2:

class Foo {
    private:
        Bar _data;
        void doIt(Bar& data);
    public:
        void doSomething() {
            doIt(_data);
        }
};

In plain english: if I have a class with a method that gets called very often, and this method defines a considerable amount of temporary data (either one object of a complex class, or a large number of simple objects), should I declare this data as private members of the class?

On the one hand, this would save the time spent on constructing, initializing and destructing the data on each call, improving performance. On the other hand, it tramples on the “private member = state of the object” principle, and may make the code harder to understand.

Does the answer depend on the size/complexity of class Bar? What about the number of objects declared? At what point would the benefits outweigh the drawbacks?

  • 1 1 Answer
  • 1 View
  • 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-12T09:52:48+00:00Added an answer on May 12, 2026 at 9:52 am

    From a design point of view, using temporaries is cleaner if that data is not part of the object state, and should be preferred.

    Never make design choices on performance grounds before actually profiling the application. You might just discover that you end up with a worse design that is actually not any better than the original design performance wise.

    To all the answers that recommend to reuse objects if construction/destruction cost is high, it is important to remark that if you must reuse the object from one invocation to another, in many cases the object must be reset to a valid state between method invocations and that also has a cost. In many such cases, the cost of resetting can be comparable to construction/destruction.

    If you do not reset the object state between invocations, the two solutions could yield different results, as in the first call, the argument would be initialized and the state would probably be different between method invocations.

    Thread safety has a great impact on this decision also. Auto variables inside a function are created in the stack of each of the threads, and as such are inherently thread safe. Any optimization that pushes those local variable so that it can be reused between different invocations will complicate thread safety and could even end up with a performance penalty due to contention that can worsen the overall performance.

    Finally, if you want to keep the object between method invocations I would still not make it a private member of the class (it is not part of the class) but rather an implementation detail (static function variable, global in an unnamed namespace in the compilation unit where doOperation is implemented, member of a PIMPL…[the first 2 sharing the data for all objects, while the latter only for all invocations in the same object]) users of your class do not care about how you solve things (as long as you do it safely, and document that the class is not thread safe).

    // foo.h
    class Foo {
    public:
       void doOperation();
    private:
       void doIt( Bar& data );
    };
    
    // foo.cpp
    void Foo::doOperation()
    {
       static Bar reusable_data;
       doIt( reusable_data );
    }
    
    // else foo.cpp
    namespace {
      Bar reusable_global_data;
    }
    void Foo::doOperation()
    {
       doIt( reusable_global_data );
    }
    
    // pimpl foo.h
    class Foo {
    public:
       void doOperation();
    private:
       class impl_t;
       boost::scoped_ptr<impl_t> impl;
    };
    
    // foo.cpp
    class Foo::impl_t {
    private:
       Bar reusable;
    public:
       void doIt(); // uses this->reusable instead of argument
    };
    
    void Foo::doOperation() {
       impl->doIt();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have following code def foo(x:Int):Unit = { if (x == 1) println
Suppose I have the following Scala code: class Foo(a: Int) class Bar(b: Buffer[Int]) extends
Suppose I have the following code: typedef struct { char **p; } STRUCT; int
Suppose I have following code package memoryleak; public class MemoryLeak { public static int
C++ novice here. I have some basic questions. In int main( int argc, char
i have tested two different varianat of similary code ,suppose i have char array
Suppose I have the following code: #include <vector> struct A { int a; int
Suppose you have the following code template<typename T,void (T::*m)(int)> struct B{ void f(T* a,int
Possible Duplicate: Are file descriptors shared when fork()ing? Suppose I have following code in
Suppose I have the following code to collect all the possible combinations. abArray =

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.