?
Solved

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

Posted on 2005-03-09
4
Medium Priority
?
173 Views
Last Modified: 2012-05-05
Hi,

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.

Shane.
0
Comment
Question by:Risky101
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 48

Accepted Solution

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

#define ARRAY_SIZE 10

class Array
{
public:
    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;
    }

private:
    int n[ARRAY_SIZE];
};


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

    a.Show();

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

    a.Show();

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

    return 0;
}

0
 

Author Comment

by:Risky101
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.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

764 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