Overloading operators: Converting method.set(data,index) into method[index]=data

Posted on 2005-03-09
Medium Priority
Last Modified: 2012-05-05

I have already overloaded the [] operator in my class, so I can use the line:

x=method[12]; //equivalent to method.get(index)

How do I overload it so that I can execute the line:

method[12]=x? // equivalent to method.set(data,index)

I can email you the sample Visual Studio .NET project if you wish. It has 50 lines of code.

I have heard that the solution may lie in using a container class, but am not sure.

Question by:Risky101
LVL 48

Accepted Solution

AlexFM earned 1000 total points
ID: 13503817
#include "stdafx.h"
using namespace std;

#define ARRAY_SIZE 10

class Array
        for ( int i = 0; i < ARRAY_SIZE; i++ )
            n[i] = 0;

    // get operator
    int operator[](int index) const
        return n[index];

    // set operator
    int& operator[](int index)
        return n[index];

    void Show()
        for ( int i = 0; i < ARRAY_SIZE; i++ )
            cout << n[i] << "  ";

        cout << endl;

    int n[ARRAY_SIZE];

int _tmain(int argc, _TCHAR* argv[])
    Array a;


    a[0] = 10;   // set
    a[1] = 20;   // set


    cout << a[0] << endl;   // get
    cout << a[1] << endl;   // get

    return 0;


Author Comment

ID: 13721855
Ah, this is perfect - thank you very much for this! Sorry for the late reply, I didn't notice it in my inbox.

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question