Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,914 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,587 people online right now. Registration is fast and FREE... Join Now!




Overloading subscript operator

 
Reply to this topicStart new topic

Overloading subscript operator, How to overload the [] operator correctly

etv1234
11 May, 2008 - 07:08 PM
Post #1

New D.I.C Head
*

Joined: 11 May, 2008
Posts: 2

CODE

#include <iostream>
#include <string>
#include <map>

using namespace std;


class Foo {
public:
        string& operator [](string);
        map <string, string> dict;
};


string&
Foo::operator[] (string a)
{
        dict[a] = "hjk";
        return dict[a];
}

int main ()
{
        Foo f;
        f["Hello"]= 123;
        cout <<f.dict["Hello"]<<endl;
}



Here the compiler doesnt give any warning and returns junk. What I'm looking for is an ability to have 123 stored as a string in the map inside my object f. I'm not supposed to specify 123 in quotes (i.e as "123") I'm trying to overload the operator [] to do this. Any suggestions??
User is offlineProfile CardPM
+Quote Post

KYA
RE: Overloading Subscript Operator
11 May, 2008 - 07:15 PM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,730



Thanked: 89 times
Dream Kudos: 1200
My Contributions
Read This

some code example from that is:

cpp

#include <iostream>
#include <string.h>
using namespace std;

class String
{
public:
String();
String(const char *const);
String(const String &);
~String();

//overloaded operators
char & operator[](int offset);
char operator[](int offset) const;
String operator+(const String&);
void operator+=(const String&);
String & operator= (const String&);
friend ostream& operator<<
(ostream& theStream, String& theString);
int GetLen()const {return itsLen;}
const char * GetString() const {return itsString;}

private:
String (int);
char * itsString;
unsigned short itsLen;
};//end String class

String::String()
{
itsString = new char[1];
itsString[0] = '\0';
itsLen = 0;
}

String::String(int len)
{
itsString = new char [len+1];
for(int i = 0; i <= len; i++)
itsString[i] = '\0';
itsLen = len;
}

String::String(const char * const cString)
{
itsLen = strlen(cString);
itsString = new char[itsLen+1];
for (int i = 0; i <itsLen; i++)
itsString[i] = cString[i];
itsString[itsLen] = '\0';
}

String::String(const String & rhs)
{
itsLen=rhs.GetLen();
itsString = new char[itsLen+1];
for (int i =0; i< itsLen; i++)
itsString[i] = rhs[i];
itsString[itsLen] = '\0';
}

String::~String()
{
delete [] itsString;
itsLen = 0;
}

String& String::operator=(const String & rhs)
{
if (this == &rhs)
return *this;
delete [] itsString;
itsLen = rhs.GetLen();
itsString = new char [itsLen +1];
for (int i = 0; i <itsLen; i++)
itsString[i] = rhs[i];
itsString[itsLen] = '\0';
return *this;
}

char & String::operator [](int offset)
{
if (offset > itsLen)
return itsString[itsLen-1];
else
return itsString[offset];
}

char String::operator [](int offset) const
{
if (offset > itsLen)
return itsString[itsLen-1];
else
return itsString[offset];
}

String String::operator +(const String & rhs)
{
int totalLen = itsLen + rhs.GetLen();
String temp(totalLen);
int i, j;
for (i = 0; i < itsLen; i++)
temp[i] = itsString[i];
for(j = 0; j < rhs.GetLen(); j ++)
temp[i] = rhs[j];
temp[totalLen]='\0';
return temp;
}

void String::operator +=(const String &rhs)
{
unsigned short rhsLen = rhs.GetLen();
unsigned short totalLen = itsLen + rhsLen;
String temp(totalLen);
int i, j;
for (i=0; i<itsLen; i++)
temp[i] = itsString[i];
for(j=0, i=0; j<rhs.GetLen(); j++, i++)
temp[i] = rhs[i-itsLen];
temp[totalLen] = '\0';
*this = temp;
}

ostream& operator<< (ostream& theStream, String& theString)
{
theStream << theString.itsString;
return theStream;
}

int main()
{
String theString("Example of Overloading Operators\n");
cout << theString;
return 0;
}


It overloads the [] operator to output a string, similar to your problem
User is online!Profile CardPM
+Quote Post

etv1234
RE: Overloading Subscript Operator
11 May, 2008 - 08:31 PM
Post #3

New D.I.C Head
*

Joined: 11 May, 2008
Posts: 2

Not sure if I understand. My problem here is since the subscript operator takes only the "key" of the dictionary as an argument, I'm unable to do the conversion for the rhs side (integer to the string). Will I need to overload the '=' operator to explicitly convert the integer to the string.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 07:57AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month