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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:29:02+00:00 2026-06-04T14:29:02+00:00

I’d like to have a class with two array subscript operator overloads: one used

  • 0

I’d like to have a class with two array subscript operator overloads: one used for reading and the other for writing.

The pourpose is to keep a counter of changes. I read (at http://faculty.cs.niu.edu/~mcmahon/CS241/c241man/node97.html) that I could do something like this:

template<typename T>
class Array
{
public:
    Array()
    {
        data = new T[100];
    }

    T &operator[] (int index)
    {
        cout << "Is writing\n";
        changes++;
        return data[index];
    }

    T operator[] (int index) const
    {
        cout << "Is reading\n";
        return data[index];
    }

private:
    T *data;
    int changes;
};

But that doesn’t work in my case. I’m using g++ 4.7 with -std=c++11 and actually only “Is writing” is printed on screen, even if I do:

Array<int> a;
a[0] = 3;
cout << a[0] << endl;

I also noticed that the latter is never called by inspecting the source with gcov.
Is the method on that page completely wrong, or is something I’m misinterpreting?

Thanks in advance.

  • 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-04T14:29:05+00:00Added an answer on June 4, 2026 at 2:29 pm

    This is only a rough idea and I haven’t thought about all the implications, but a proxy class with overloaded assignment and cast operators could solve your problem:

    template<typename T>
    class CountingProxy
    {
    public:
        CountingProxy(int& counter, T& ref) : counter_(counter), ref_(ref)
        {
        }
    
        CountingProxy<T>& operator=(const T& o)
        {
            cout << "Is writing\n";
            counter_++;
            ref_ = o;
            return *this;
        }
    
        operator T()
        {
            cout << "Is reading\n";
            return ref_;
        }
    
    private:
        int& counter_;
        T& ref_;
    };
    
    template<typename T>
    class Array
    {
    public:
        Array()
        {
            data = new T[100];
        }
    
        CountingProxy<T> operator[] (int index)
        {        
            return CountingProxy<T>(changes, data[index]);
        }
    
        T operator[] (int index) const
        {
            cout << "Is reading\n";
            return data[index];
        }
    
    private:
        T *data;
        int changes;
    };
    

    On the other hand, you probably would be better off by implementing separate functions for reading and writing elements of your array, such as T& get(int index), const T& get(int index) const and void put(int index, const T& value).

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have some data like this: 1 2 3 4 5 9 2 6
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace

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.