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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:18:50+00:00 2026-05-25T22:18:50+00:00

Can anyone please explain the difference between: const char& operator[] const and char& operator[]

  • 0

Can anyone please explain the difference between:

const char& operator[] const

and

char& operator[]

in C++?
Is it true that the second one is duplicating the string? and why?

  • 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-25T22:18:51+00:00Added an answer on May 25, 2026 at 10:18 pm

    They both return references to the internal member of the string.

    The first method is defined as a const method (the last const) and as such promises not to change any members. To make sure you can;t change the internal member via the returned reference this is also const.

      const char& operator[](int i) const
    //                              ^^^^^ this means the method will not change the state
    //                                    of the string.
    
    //^^^^^^^^^^^  This means the object returned refers to an internal member of
    //             the object. To make sure you can't change the state of the string
    //             it is a constant reference.
    

    This allows you to read members from the string:

    std::string const  st("Plop is here");
    
    char x  = st[2];          // Valid to read gets 'o'
    st[1]   = 'o';            // Will fail to compile.
    

    For the second version it say we return a reference to an internal member. Neither promise that the object will not be altered. So you can alter the string via the reference.

       char& operator[](int i)
    // ^^^^^  Returns a reference to an internal member.
    
    std::string mu("Hi there Pan");
    
    char y = mu[1];           // Valid to read gets 'i'
    mu[9]  ='M';              // Valid to modify the object.
    std::cout << mu << "\n";  // Prints Hi there Man
    

    Is it true that the second one is duplicating the string? and why?

    No. Because it does not.

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

Sidebar

Related Questions

Can anyone please explain to me what is the difference between IEnumerable & IEnumerator
Rails newbie here. Can anyone please explain the difference to me between the following
Can any one explain what is the difference between document.all and the document.forms[0] please?
Can anyone please explain what's the difference between the two kinds of getView() implementation?
can anyone please explain the concept of dispatcher, is it one dispatcher per thread
can anyone please explain how this works (asz + 7) & ~7; It rounds
can any one explain the difference between the mvc and web application in asp.net.
Possible Duplicate: Java Generics Hi, Can anyone please explain the difference of the three
Can anyone please explain to me why the following two queries yield different results?
Can anyone please explain me what is DataContractTranslstor and what's it's use?

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.