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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:30:20+00:00 2026-06-02T09:30:20+00:00

supposed there are two overloaded member function(a const version and a non-const version) in

  • 0

supposed there are two overloaded member function(a const version and a non-const version) in the String class:

char & String::operator[](int i)         //Version 1
{
    cout<<"char & String::operator[](int i) get invoked."<<std::endl;
    return str[i];
}


const char & String::operator[](int i) const       //Version 2
{

    cout<<"const char & String::operator[](int i) const get invoked."<<std::endl;
    return str[i];
}

and there is a test code fragment

int main(){
    String a;
    cout<<a[0]<<endl;  //Line 1
    a[0]='A';     //Line 2
}

How does the compiler decide which function to call? I found that Version 1 always got called when I run the program. Could anybody tell me why it is? And how can Version 2 got called?

  • 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-02T09:30:28+00:00Added an answer on June 2, 2026 at 9:30 am

    If the object is const, the the const member function will be called. If the object is non-const the non-const member-function is called.

    Exception
    If their is only the const function, it is called in any case.

    #include <iostream>
    using namespace std;
    
    class Foo {
    public:
        void print() {
            cout << "Foo non-const member function\n";
        }
        void print() const {
            cout << "Foo const member function\n";
        }
    };
    
    class Bar {
    public:
            void print() const {
                    cout << "Bar const member function\n";
            }
    };
    
    
    int main() {
        Foo foo_non_const;
        const Foo foo_const;
    
        Bar bar_non_const;
        const Bar bar_const;
    
        foo_non_const.print();
        foo_const.print();
    
        bar_non_const.print();
        bar_const.print();
    
        return 0;
    }
    

    $ ./blah

    Foo non-const member function
    Foo const member function
    Bar const member function
    Bar const member function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming following code. There is class MyStream witch has template overloaded operator <<. There
There is supposed to be a comma between these two strings foo = ['dumb'
Suppose I have a rather large class Matrix , and I've overloaded operator== to
I have my site under the latest version of Jquery. There's two problems however:
Suppose there are two lists a = {a1, a2, a3} and b = {b1,
Suppose that there're two types of packet in the chatting server. Those two packets
Suppose you have two linked servers called Local and Remote respectively. Is there a
Suppose I have a string like this: one two three four five six seven
I have a script that is supposed to sit there, happily running in a
Is there some agreed upon algorithm of what is supposed to happen when, in

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.