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

The Archive Base Latest Questions

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

In order to help avoid unnecessary heap allocations of strings inside a class, I

  • 0

In order to help avoid unnecessary heap allocations of strings inside a class, I have adopted a pattern similar to the following:

#include <algorithm>
using namespace std;

class Person
{
public:
    template<typename Iter>
    void GetName(Iter iter) const     // Allow for caller to provide the buffer
    {
        const char *name = ...;  // Get the name
        size_t cchName = ...;  // Get the size
        copy(&name[0], &name[cchName], iter);
    }

    string GetName() const            // Convenience method
    {
        string name;
        this->GetName(inserter(name, name.end()));
        return name;
    }
};

However, the code also seems to work perfectly fine when I say

    void GetName(const Iter &iter) const     // <----- changed to const &

Is there any reason (performance or otherwise) for me to use a const & version of the iterator, or should I just use Iter itself? (I don’t know the convention for iterators, or if there are any implications.) (C++03)

  • 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-26T15:24:42+00:00Added an answer on May 26, 2026 at 3:24 pm

    The difference here is same difference as pass by value and pass by reference while calling functions.

    When you pass by value an copy of the passed variable gets created and the function uses it.

    When you pass by reference, the copy is not made. Also, passing the type as const guards it against modification to some level(ofcourse hackery can break it).

    Since In your code example you do not need to iterate using the iterator and just need to pass it to std::copy which creates its own copy, So your iterator being a const doesn’t matter to it, Hence passing by const reference is better option in your example.

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

Sidebar

Related Questions

I have been creating a helper class for the Facebook PHP API in order
Have simple Order - Item app. No cart. Need help with joined scopes for
I found a similar question here, but didn't help me. I basically have apache
I have this code : public class OrderModel { public List<Order> Orders { get;
Visual Studio added code analysis ( /analyze ) for C/C++ in order to help
Order by descending is not working on LINQ to Entity In the following Query
In order to know how many times a pattern exists in current buffer, I
In order to debug an asp.net web app I have to have IE Script
In order to improve my open source project, I need testers. I have created
I need your help to optimize the query to avoid using Using filesort.The job

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.