Re: [ROOT] Operator overloading [silly question]

From: Stephen J. Gowdy (gowdy@SLAC.stanford.edu)
Date: Fri Apr 26 2002 - 14:14:12 MEST


You'd make this one;

const TString& operator[]( const char* key ) const;

and then create a new one;

TString& operator[]( const char* key );

On Fri, 26 Apr 2002, Ashley Cambrell wrote:

> Hi Rooters,
> 
> Just a simple question.  I'm still learning so I'm sorry if this is one
> of those "What's an int?" questions.  I have a class the inherits from
> TMap and I want to emulate associative arrays.  
> 
> I have this:
> 
>     TString operator[](const char* key)
>     {
>         TObjString *tosKey = new TObjString(key);
>         TString value;
>        
>         if( this->TMap::GetValue(tosKey) )
>             value = ((TObjString
> *)(this->TMap::GetValue(tosKey)))->String();
>        
>         delete tosKey;
>        
>         return value;
>     }
> root [1] THTTPData td;                                         
> root [2] td.Add(new TObjString("bob"), new TObjString("smith"));
> root [3] cout << td["bob"] << endl;                            
> smith
> root [4]
> 
> which works fine, but I'd like to also be able to do
> 
> td["jane"] = "anderson";
> 
> Is it possible?  (add another operator overload? but then how does C++
> tell the difference between the two)
> 
> Thanks for your patience :-)
> 
> Ashley Cambrell
> 
> 

-- 
 /------------------------------------+-------------------------\
|Stephen J. Gowdy                     | SLAC, MailStop 34,       |
|http://www.slac.stanford.edu/~gowdy/ | 2575 Sand Hill Road,     |
|http://calendar.yahoo.com/gowdy      | Menlo Park CA 94025, USA |
|EMail: gowdy@slac.stanford.edu       | Tel: +1 650 926 3144     |
 \------------------------------------+-------------------------/



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:51 MET