Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 8th, 2006, 4:49 PM   #1
sackarias
Programmer
 
sackarias's Avatar
 
Join Date: Jan 2006
Posts: 58
Rep Power: 3 sackarias is on a distinguished road
operator overloading question..

Trying to understand operator overloading..

const string &operator[](int idx) const;



What do the const's do in this case? Primarily the one at the end, I see it being used like that in the examples I have and I can't make a connection with why/how it's like that.

Thanks!
sackarias is offline   Reply With Quote
Old Mar 8th, 2006, 5:02 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
The trailing const specifies that the function does not modify the object for which it is invoked.

EDIT: Scorpions4ever, a sometime contributor here, has a good tutorial on operator overloading .
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 8th, 2006, 7:20 PM   #3
sackarias
Programmer
 
sackarias's Avatar
 
Join Date: Jan 2006
Posts: 58
Rep Power: 3 sackarias is on a distinguished road
Quote:
Originally Posted by DaWei
The trailing const specifies that the function does not modify the object for which it is invoked.

EDIT: Scorpions4ever, a sometime contributor here, has a good tutorial on operator overloading .
So the function basically can only go into the object and "inspect", and in this case return a string?

Also, very good call on that tutorial, it helped clear up a lot of usability issues I had.
sackarias is offline   Reply With Quote
Old Mar 8th, 2006, 7:29 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You probably want to read up on 'const' a little bit. Bear in mind that depending on exactly what its relationship to a object is, it means different things. For instance, you might not be able to modify the reference, but you can modify the referenced object. On the other hand, it could be vice versa, if expressed differently. Also bear in mind that const is a keyword and actually an instruction or hint to the compiler, who's task it is to prevent you from messing with these things you have declared protected (constant). Typically, you don't want to depart from this initial design decision, but it's certainly possible to finagle around with these things.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 8th, 2006, 7:52 PM   #5
sackarias
Programmer
 
sackarias's Avatar
 
Join Date: Jan 2006
Posts: 58
Rep Power: 3 sackarias is on a distinguished road
Good points. Thanks for your help.
sackarias is offline   Reply With Quote
Old Mar 9th, 2006, 1:25 AM   #6
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
The leading const specifies that the reference returned is const i.e. the function returns a reference to a string that is logically constant.
grumpy is offline   Reply With Quote
Old Mar 9th, 2006, 5:36 AM   #7
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Finagle..nice word.

When would a function returning a reference not be constant?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Mar 9th, 2006, 3:18 PM   #8
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
An indexing operator maybe?
Jimbo is offline   Reply With Quote
Old Mar 9th, 2006, 4:30 PM   #9
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Quote:
Originally Posted by nnxion
When would a function returning a reference not be constant?
When it makes sense for the caller to be allowed to change the object referred to.

For example;
class ResizeableIntArray
{
     public:

          int &operator[](int index);

          const int &operator[](int index) const;

     // other details eliminated

     private:

         void Resize(index);    // resize array so index identifies a valid element
                                      //   any added elements assigned value of zero

         bool ValidIndex(int index) const;  // check if index identifies a valid element

         void ComplainBitterly() const;   // report an error condition (eg throw exception)
         
};

//   the version without const adornment

int ResizableIntArray::operator[](int index)
{
      //   the following is pseudo-code to illustrate idea
      if (InvalidIndex(index))
          ResizeArray(index);      //   this won't work if method is const (i.e. trailing const)

      if (InvalidIndex(index)) ComplainBitterly();

      return element[index];      
}

//   the version with const adornment

const int ResizableIntArray::operator[](int index) const
{
      if (InvalidIndex(index)) ComplainBitterly();
      return element[index];      
}

int main()
{
       ResizeableIntArray modifiable;

       modifiable[3] = 5;     // calls non-const version

       int &x = modifiable[5];    //   this actually resizes array.
                                        //      x will reference 5'th element

       x = 10;   // will set 5th element to 10

       const ResizeableIntArray unmodifiable;

       unmodifiable[3] = 42;   //  compiler will compain as const operator []
                                      // used here, so can't be used as LHS

       int &y = unmodifiable[5];   // compiler will complain.  const operator[]
                                           // called, but a const returned reference
                                           // can't be bound to a non-const one.

       const int &z = unmodifiable[5];   // this is OK (const operator [], result
                                                   // assigned to const reference.
                                                   //  bitter complaint at run time unless
                                                   //  unmodifiable has a 5th element

       int q = z;         // OK;  assign q to value referenced by z
       int &r = z;       //  compiler complaint;  binding const ref to non-const one

       const int &s = z;  //  OK.  s and z are alias for unmodifiable[5]
                               //  (if it exists and we get here despite complaint 
                               //  above)
}
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:00 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC