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

The Archive Base Latest Questions

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

How to translate properly the following Java code to C++? Vector v; v =

  • 0

How to translate properly the following Java code to C++?

Vector v;
v = getLargeVector();
...
Vector getLargeVector() {
    Vector v2 = new Vector();
    // fill v2
    return v2;
}

So here v is a reference. The function creates a new Vector object and returns a reference to it. Nice and clean.

However, let’s see the following C++ mirror-translation:

vector<int> v;
v = getLargeVector();
...
vector<int> getLargeVector() {
    vector<int> v2;
    // fill v2
    return v2;
}

Now v is a vector object, and if I understand correctly, v = getLargeVector() will copy all the elements from the vector returned by the function to v, which can be expensive. Furthermore, v2 is created on the stack and returning it will result in another copy (but as I know modern compilers can optimize it out).

Currently this is what I do:

vector<int> v;
getLargeVector(v);
...
void getLargeVector(vector<int>& vec) {
    // fill vec
}

But I don’t find it an elegant solution.

So my question is: what is the best practice to do it (by avoiding unnecessary copy operations)? If possible, I’d like to avoid normal pointers. I’ve never used smart pointers so far, I don’t know if they could help here.

  • 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-13T08:45:25+00:00Added an answer on May 13, 2026 at 8:45 am

    Most C++ compilers implement return value optimization which means you can efficiently return a class from a function without the overhead of copying all the objects.

    I would also recommend that you write:

    vector<int> v(getLargeVector());
    

    So that you copy construct the object instead of default construct and then operator assign to it.

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

Sidebar

Related Questions

Help me translate following block of the Haskell code. The run function produces text
I have to translate some code from C# to Java and I have the
I'm trying to translate the following JS code to ObjC; It basically defines a
Can any body translate the following c# code to vb. I have tried telarik
I need to translate the following Code to an Expression and I will explain
The following code works properly <!DOCTYPE html> <html> <head> <title>Working</title> <style type=text/css> html {
Namely, how does the following code: var sup = new Array(5); sup[0] = 'z3ero';
I would like to know how to translate the following code to codebehind instead
I am trying to translate a function in a book into code, using MATLAB
Does anyone have a translate function for x/y positions after rotation in javascript? for

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.