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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:13:48+00:00 2026-06-10T00:13:48+00:00

In Effective C++ Item 03, Use const whenever possible. class Bigint { int _data[MAXLEN];

  • 0

In Effective C++ Item 03, Use const whenever possible.

class Bigint
{
  int _data[MAXLEN];
  //...
public:
  int& operator[](const int index) { return _data[index]; }
  const int operator[](const int index) const { return _data[index]; }
  //...
};

const int operator[] does make difference from int& operator[].

But what about:

int foo() { }

and

const int foo() { }

Seems like that they’re the same.

My question is, why we use const int operator[](const int index) const instead of int operator[](const int index) const ?

  • 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-10T00:13:49+00:00Added an answer on June 10, 2026 at 12:13 am

    Top level cv-qualifiers on return types of non class type are ignored.
    Which means that even if you write:

    int const foo();
    

    The return type is int. If the return type is a reference, of course,
    the const is no longer top level, and the distinction between:

    int& operator[]( int index );
    

    and

    int const& operator[]( int index ) const;
    

    is significant. (Note too that in function declarations, like the above,
    any top level cv-qualifiers are also ignored.)

    The distinction is also relevant for return values of class type: if you
    return T const, then the caller cannot call non-const functions on the
    returned value, e.g.:

    class Test
    {
    public:
        void f();
        void g() const;
    };
    
    Test ff();
    Test const gg();
    
    ff().f();             //  legal
    ff().g();             //  legal
    gg().f();             //  **illegal**
    gg().g();             //  legal
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Effective Java: Item 6: Eliminate obsolete object references. Generally speaking, whenever a class manages
// The worst possible legal hash function - never use! @Override public int hashCode()
From Effective Java Second Edition, Item 28 : Do not use wildcard types as
In Scott Meyers's Effective C++ , item 18 Make interfaces easy to use correctly
In Effective C++ (Item 18: Make interfaces easy to use correctly and hard to
I am reading Bill Wagner's book Effective C# . In Item 32 he is
At the end of Item 52 (Customising new and delete) in Myer's Effective C++
Consider the following snippet: import java.util.*; public class EqualsOverload { public static void main(String[]
Item 3 of Josh Block's Effective Java (Enforce the Singleton Property With a Private
I am reading Effective C# by Bill Wagner. In Item 14 - Minimize Duplicate

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.