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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:30:25+00:00 2026-05-11T08:30:25+00:00

I am curious about prevalent methodologies. I found myself doing both of these things

  • 0

I am curious about prevalent methodologies.

I found myself doing both of these things interchangeably:

Note in all cases the object is allocated locally.

std::string GetDescription () {       std::string desc;       /* Create a description */         return desc; }   void GetResult (map<int, double> & resultMap) {         /*Fill map result map with  values*/  }  map<int, double> GetResult2(map<int, double> & resultMap) {           map<int, double> & resultMap         /* Fill map result map with  values */           return resultMap; } 

What is the preferred way to do this?

  • 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. 2026-05-11T08:30:26+00:00Added an answer on May 11, 2026 at 8:30 am

    It greatly depends on the task at hand. I would go for the first approach for functions that create new values while using the second approach for functions that change the parameters. As mentioned by others, in some cases other restrictions (cost of creation / assignment) can affect the decision.

    The first definition is clearly stating that it will create a new object out of nothing and will return it. That is, the same definition is stating the semantics of the call.

    The second and third definitions require the caller to create an object before calling the function. This can be a hassle to the user as she cannot just ignore the return value (or use it as an unnamed temporal to feed another function, see snippet 1), and can be limited in cases (i.e. cannot reassign a reference from an already created object, see snippet 2).

    It is not clear from the function definition what is the use of the parameter inside the function. Will the behavior of the function differ depending on the passed object? It is not clear from the function definition.

    The third type is, I believe, the worst option. Not only it forces the caller to create a named variable to pass to the function but also makes a copy to return (unless it was a typo and you are returning a [possibly const] reference)

    At any rate, other considerations can unbalance the advantage of clear semantics that the first approach provides. In some cases performance, or other limitations like objects that cannot be copied (see snippet 3)

    // snippet 1 //    user code to the different approaches class X {}; X f1(); void f2( X& ); X f3( X& ); void g( X const & );  void test1() {    g( f1() ); // valid code } void test2() {    X x; // must create instance before    f2( x ); // then call the function    g( x ); } void test3() {    X x; // must create instance before    g( f3( x ) ); }  // snippet 2 class Y {}; class X { public:    X( Y & y ) : y_( y ) {} private:    Y & y_; }; X f1(); void f2( X & ); X f3( X & );  void test1() {    X x = f1(); } void test2or3() {      Y y;    X x( y ); // user must create with a reference    f2( x ); // f2 cannot change the reference inside x to another instance }  // snippet 3 class X { private:    X( X const & );    X& operator= ( X const & ); }; X f1(); // Returned object cannot be used void f2( X & ); X f3( X & );  void test1() {    X x = f1(); // compilation error } void test2() {    X x;    f2( x ); } void test3() {    X x;    X y = f3( x ); // compilation error } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you running on OS 3.0? I saw the same… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer It looks like you need to register Apache::Session::Memcached with Apache::Session::Wrapper,… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer Use DATENAME or DATEPART: SELECT DATENAME(dw,GETDATE()) -- Friday SELECT DATEPART(dw,GETDATE())… May 12, 2026 at 1:19 am

Related Questions

I've seen plenty of sites that provide data on which browsers and versions of
I am currently writing a CRUD class in PHP using PDO. I like the
I am curious about how many of you folks incorporate mocking of objects (frameworks
I am curious about how do you protect your software against cracking, hacking etc.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.