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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:42:38+00:00 2026-05-25T15:42:38+00:00

When a function (callee) returns an object to the caller function, is it returned

  • 0

When a function (callee) returns an object to the caller function, is it returned by
value or by reference?

I have written a function which builds a large vector when called. I want to return this vector to the calling main function by constant reference so I can do some further processing on it.

I was in doubt because I was told that when a C++ function returns and terminates, all the variables/memory associated with that function, get wiped clean.

struct node {
    string key;
    int pnum;
    node* ptr;
};

vector<vector<node>> myfun1(/* Some arguments */) {
    /* Build the vector of vectors. Call it v. */
    return v;
}

int main() {
    a = myfun1(/* Some arguments */)
}
  • 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-25T15:42:39+00:00Added an answer on May 25, 2026 at 3:42 pm

    C++ functions can return by value, by reference (but don’t return a local variable by reference), or by pointer (again, don’t return a local by pointer).

    When returning by value, the compiler can often do optimizations that make it equally as fast as returning by reference, without the problem of dangling references. These optimizations are commonly called “Return Value Optimization (RVO)” and/or “Named Return Value Optimization (NRVO)”.

    Another way to for the caller to provide an empty vector (by reference), and have the function fill it in. Then it doesn’t need to return anything.

    You definitely should read this blog posting: Want Speed? Pass by value.

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

Sidebar

Related Questions

In an actionscript function (method) I have access to arguments.caller which returns a Function
I have a class called User with static function loginRequired(), which returns false if
I have a function called FindSpecificRowValue that takes in a datatable and returns the
I am using a library function called get_count_and_price which returns a 2-tuple (count,price). In
I have the following function which is being called as the start to a
When a function is attached to an object and called: function f() { return
The function I called inside jquery returns undefined. I checked the function and it
Windows API ::FindWindow function fails when called from Service application. GetLastError() also returns 0
I'd like to build a function that returns false if it's been called less
I have an object which I create from the response from a webservice. If

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.