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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:28:00+00:00 2026-06-15T04:28:00+00:00

I have a doubt on returning std::string as const reference. class sample { public:

  • 0

I have a doubt on returning std::string as const reference.

class sample
{
public:
  std::string mString;
  void Set(const std::string& s)
  {
    mString = s;
  }
  std::string Get()
  {
    return mString;
  }
 };

In the Set function I am passing the std::string as const reference, const because its value is not changing inside the function.

And In Get function, actually I am confused here. Return std::string as value makes more sense. But I am not sure that, by passing the string as const reference makes any advantages. By returing string as reference will increase the exectuion speed, I think So, but I am not sure. But returning it as ‘const makes any benefit for 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. Editorial Team
    Editorial Team
    2026-06-15T04:28:01+00:00Added an answer on June 15, 2026 at 4:28 am

    Returning by reference or const reference has no speed difference – both are very fast as they just return a reference to the original object, no copying is involved.

    An object returned by (non-const) reference can be modified through that reference. In your specific example, mString is public, so it can be modified anyway (and directly). However, the usual approach with getters and setters (and the primary reason for their introduction) is encapsulation – you only allow access to your data members through the getter/setter, so that you can detect invalid values being set, respond to value changes and just generally keep the implementation details of your class hidden inside it. So getters normally return by const reference or by value.

    However, if you return by const reference, it binds you to always keep an instance of std::string in your class to back up the reference. That is, even if you later want to redesign your class so that it computes the string on the fly in the getter instead of storing it internally, you can’t. You’d have to change your public interface at the same time, which can break code using the class. For example, as long as you return by const-reference, this is perfectly valid code:

    const std::string *result = &aSample.Get();
    

    This code will of course produce a dangling pointer no longer compile if Get() is changed to return by value instead of const reference. (thanks to Steve Jessop for correcting me)

    To sum up, the approach I would take is to make mString private. Get() can return by value or by const-reference, depending on how certain you are that you’ll always have a string stored. The class would then look like this:

    class sample
    {
      std::string mString;
    
    public:
      void Set(const std::string &s)
      {
        mString = s;
      }
      std::string Get() const
      {
        return mString;
      }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What is the difference between const and readonly? i have doubt in
I have to set up 2 database for one application. I did & working
I have a data class with the following methods: ExecuteUDIQuery(string query) ExecuteSelectQuery(string query) ExecuteSP(string
I have doubt in creating objects in C++. Say I have a class called
I have doubt with android backstack , please consider the scenario I have two
I have doubt when i am reading difference resource for Proxy fetching or No-proxy
I am reading linux device driver book of rubini,corbet and hartmen.I have doubt regarding
I have some doubt on below code #include<stdio.h> int i=6; int main() { int
I have no doubt this has been asked and answered here a dozen times,
I have a doubt, when we try to register for C2DM, does the registration

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.