Overloading the const and non-const Member Functions

We all know about the function overloading feature in C++, which allows us to have the same name given for a function with different signatures (here, different signature means different types or number of arguments for that function). But what about overloading based on the const-ness of a member function? Take the following function:

    class MyArray    {    private:        int m_iInts[100];  // Array of integers    public:        int& operator [](int iIndex);  // Overloading subscript operator    };    int& MyArray::operator[](int iIndex) // Definition    {        return m_iInts[iIndex];    }

You are trying to create an integer array class, with an overloaded operator function for the subscript access of the array. You should be able to say:

    MyArray objArray;    objArray[10] = 100;

This establishes a call to the operator[](int) member function, which returns a reference to the requested array member. It also assigns a value to that particular array member. However, consider the situation below:

    void PrintArray(const MyArray &obj)    {        // Code for printing the elements        cout<

Assuming that this is a function for printing the array elements, this takes a constant reference to MyArray instance as a parameter. Here's the problem: when you try to print the value by saying obj[10], you won't be able to access the non-const version of operator[](int), because the object is of constant type. The compiler will complain about this, saying:

    no operator defined which takes a left-hand operand of type 'const class MyArray'

In this situation, you need to declare another version of operator[](int), specifically for const objects:

    const int MyArray::operator[](int index) const    {        return m_iElements[index];    } 

This is a const member function, meaning not supposed to change the value of any member variable. Hence, you have two versions of member functions with the same name and even the same argument, but different in const-ness.

    int& operator [](int iIndex);                 // non-const version    const int operator[](int index) const;    // const version 

The first version should be be used for all non-const objects, and the latter only used for const objects.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may