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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:15:47+00:00 2026-06-11T17:15:47+00:00

Considering the following piece of code. #include <iostream> using namespace std; class Object {

  • 0

Considering the following piece of code.

#include <iostream>
using namespace std;

class Object
{
public:
    Object() {}

    void Print() const
    {
        cout << "const" << endl;
    }

    void Print()
    {
        cout << "mutable" << endl;
    }
};

void print_obj(const Object& obj)
{
    obj.Print();
}

int main()
{
    Object       obj1;
    const Object obj2;
    Object*const pobj1 = &obj1;
    
    print_obj(obj1);
    print_obj(obj2);
    
    obj1.Print();
    obj2.Print();
    
    pobj1->Print();
    
    return 0;
}

The output is

const
const
mutable
const
mutable

I am wondering, how does C++ decide which method to invoke, when facing many mutable methods with the same name?

  • 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-11T17:15:48+00:00Added an answer on June 11, 2026 at 5:15 pm
    print_obj(obj1); 
    print_obj(obj2); 
    

    The function to be called is evaluated on the basis of the cv-qualifier(const/volatile) of the passed object. Note that cv-qualifiers are considered while function overload resolution.
    If passed object is const, function receiving const argument is selected.If passed object is non-const then function receiving non-const argument is selected.


    obj1.Print();   
    obj2.Print();   
    pobj1->Print();
    

    If the object is const then only call const member function can be called.
    If the object is non-const then the non-const version is prefferred over the const version.

    The rules are clearly specified by the standard.

    Reference:
    C++03 standard:
    §13.3.1 Candidate functions and argument lists:

    For non-static member functions, the type of the implicit object parameter is “reference to cv X” where X is the class of which the function is a member and cv is the cv-qualification on the member function declaration. [Example: for a const member function of class X, the extra parameter is assumed to have type “reference to const X”. ]

    So if the object is const compiler will pick version of member function which has implicit object parameter of type reference to const Object which is const version of Print().

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

Sidebar

Related Questions

Considering following code public class A { public static void main(String[] args) { new
Considering following code: class Results { public int playerId; public int score; public int
Considering the following sample code: // delivery strategies public abstract class DeliveryStrategy { ...
I got the following piece of code: public class Collect { public string name{
Considering the following basic event-related code inside a class: public event EventHandler Updated; public
Considering that simple java code which would not work: public class Bar extends AbstractBar{
Considering the following code public bool GetFalse() { return false; } public bool GetTrue()
Considering the following (pointless, but it's for illustration purpose) test class : public class
Considering the following case in C#: class Foo { public Foo() { } }
Considering the following: public class Person { public Person(string fName, string lName) { this.firstName

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.