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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T13:56:56+00:00 2026-05-21T13:56:56+00:00

I define two versions of overloaded operator[] function in a class array . ptr

  • 0

I define two versions of overloaded operator[] function in a class array. ptr is a pointer to first element of the array object.

int& array::operator[] (int sub) {  
   return ptr[sub];
} 

and

int array::operator[] (int sub) const { 
  return ptr[sub];
}

Now, if I define a const object integer1 the second function can only be called….. but if I make a non-const object and then invoke as below:

cout << "3rd value is" << integer1[2];

which function is called here?

  • 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-21T13:56:56+00:00Added an answer on May 21, 2026 at 1:56 pm

    In your second example, the non-const version will be called, because no conversion is required, and a call that requires no conversion is a better match than one that requires a conversion.

    Ultimately, however, you have a basic problem here: what you really want is behavior that changes depending on whether you’re using your object as an rvalue or an lvalue, and const doesn’t really do that. To make it work correctly, you normally want to return a proxy object, and overload operator= and operator T for the proxy object:

    template <class T>
    class myarray { 
        T *ptr;
    
        class proxy { 
            T &val;
            proxy &operator=(proxy const &p); // assignment not allowed.
        public:
            proxy(T &t) : val(t) {}
            operator T() const { return val; }
            proxy &operator=(T const&t) { val = t; return *this; }
        };
    
        proxy const operator[](int sub) const { return proxy(ptr[sub]); }
        proxy operator[](int sub) { return proxy(ptr[sub]); }
        // obviously other stuff like ctors needed.
    };
    

    Now we get sane behavior — when/if our array<int> (or whatever type) is const, our operator[] const will be used, and it’ll give a const proxy. Since its assignment operators are not const, attempting to use them will fail (won’t compile).

    OTOH, if the original array<int> was not const, we’ll get a non-const proxy, in which case we can use both operator T and operator=, and be able to both read and write the value in the array<int>.

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

Sidebar

Related Questions

I often find myself having to define two versions of a function in order
I'm doing a benchmark with two versions of a function to filter a linked
I have two classes: class Object { public: Object(); virtual void update(); virtual void
I'm doing a audit class. For that I've two object of same type, but
I have a class with two methods defined in it. public class Routines {
Suppose a header file defines a function template. Now suppose two implementation files #include
I am releasing two versions of an app--free and paid. The differences between the
I'm trying to define a generic residue class ring in Scala. A residue class
I have two different versions of git. In the 1.6.2 version, git push does
IIS7: How to define that windows authentication is turned on? I know that two-stage

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.