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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:34:09+00:00 2026-05-28T20:34:09+00:00

Comming from Java I would like to implement the following test multiple implementations idiom

  • 0

Comming from Java I would like to implement the following “test multiple implementations” idiom in C++

void testMethod() {
    List veryBigList = createNewRandomList(10000);
    Algo algo = createAlgo(veryBigList);
    algo.run();
}

// this can be overwritten from a subclass to test against different algo
Algo createAlgo(List list) {
    Algo a(list);
    return a;
}

List createNewRandomList(int size) {
    List l = new ArrayList(size);
    // fill list with random objects
    return l;
}

I’m a bit lost how to do this the right way in C++ … when I’m doing it this way:

void test_method() {
    vector<string> big_list(10000);
    init_random_list(big_list);
    algo my_algo = create_algo(big_list);
    my_algo.run();
}

algo create_algo(vector<string> list) {
    algo a(list);
    return a;
}

void init_random_list(vector<string> list) {
    // fill list with random objects        
}

then what will C++ do? Will it copy the full list to go to create_algo and then on return it’ll copy the algo (and the list again)??

But when using call by reference I would access the reference in test_method outside of the scope of algo causing a lot trouble and violating the RAII principle, right?

I know for some implementations of C++ they optimize speed for call by value according to this doc, but can I rely on that for g++ and visual c++?

  • 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-28T20:34:10+00:00Added an answer on May 28, 2026 at 8:34 pm

    Assuming that the code is exactly as you posted, and that algo can be modified to store a reference, then you probably want to pass and store a reference.

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

Sidebar

Related Questions

Coming from the Java world to the iPhone, I would like to see the
Coming from Java background I am guessing this is expected. I would really love
I would like to implement a simple in-memory LRU cache system and I was
I'm currently learning ObjC and Cocoa programming, coming from the Java world. To test
I just started learning Ruby coming from Java. In Java you would use packages
I'm comming from Java world mainly. So, C# properties do look nice. I know
I'm coming from the Java world and are building a small c++ program at
I'm brand new to java, coming from a ruby world. One thing I love
I am coming from Java and am currently working on a C# project. What
I'm coming from Java/C++ to Ada and am having trouble figuring out the small

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.