![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jan 2006
Posts: 58
Rep Power: 3
![]() |
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! |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 | |
|
Programmer
Join Date: Jan 2006
Posts: 58
Rep Power: 3
![]() |
Quote:
Also, very good call on that tutorial, it helped clear up a lot of usability issues I had. |
|
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Jan 2006
Posts: 58
Rep Power: 3
![]() |
Good points. Thanks for your help.
|
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
The leading const specifies that the reference returned is const i.e. the function returns a reference to a string that is logically constant.
|
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#8 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3
![]() |
An indexing operator maybe?
|
|
|
|
|
|
#9 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Quote:
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)
} |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|