Jump to content
  • Advertisement
Sign in to follow this  
adder_noir

overloading const and the subscript operator

This topic is 2917 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been working through a link provided by a moderator regarding const stuff. I have got through most of it on my own, hard work but I did it. There are some parts of the next bit of code that I am not too sure of particularly the subscript operator:

class Fred { ... };

class MyFredList {
public:
Fred const& operator[] (unsigned index) const;
Fred& operator[] (unsigned index);
...
};




I have never seen this before. Exactly how does the following line work:

Fred const& operator[] (unsigned index) const;


I would appreciate some insight into this before I proceed with my next queries, thanks.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by adder_noir
I have never seen this before. Exactly how does the following line work:

Fred const& operator[] (unsigned index) const;


I would appreciate some insight into this before I proceed with my next queries, thanks.


You have an operator overload that takes an unsigned index. The const at the end is saying that this method does not mutate the state of the MyFredList. It returns a reference to the constant Fred object that you index.


MyFredList temp;
DisplayFred(temp[0]);
DisplayFred(const Fred & blah) {
cout << ...
}


Since you have both const and non-const versions, the compiler will use the const version when it can, and fall back on the non-const version if you need to mutate the state of your stored Fred objects.

Share this post


Link to post
Share on other sites
What about it are you confused about? The first const?, the second? or the operator []?

The first is the same as saying const Fred&, AFAIK, which specifies that the reference to fred is immutable, though I believe a references are already const, someone more knowledgeable will correct me if I'm wrong.

The second const specifies that this member function does not modify any part of the MyFredList object.

EDIT:: Triple Ninja'd

Share this post


Link to post
Share on other sites
Quote:
Original post by adder_noir
I have never seen this before. Exactly how does the following line work:

Fred const& operator[] (unsigned index) const;


I would appreciate some insight into this before I proceed with my next queries, thanks.


It allows the compiler to select the appropriate operator depending on its usage:

class Array {
public:
int& operator[] ( int idx ) {
return arr[idx];
}

const int& operator[] ( int idx ) const {
return arr[idx];
}

private:
int arr[10];
};

// With our custom class ...
void func1( const Array& a ) {
a[0] = 1; // Error, can't use int& Array::operator[], since a is const
cout << a[0]; // Ok, calls const int& Array::operator[] const
}

void func2( Array& a ) {
a[0] = 1; // Ok, calls int& Array::operator[]
cout << a[0]; // Ok, calls const int& Array::operator[] const
}

// With a raw array...
void test1( const int* arr ) {
arr[0] = 1; // Error
cout << arr[0]; // Ok
}

void test2( int* arr ) {
arr[0] = 1; // Ok
cout << arr[0]; // Ok
}

int main() {
Array myArray;

func1( myArray );
func2( myArray );

int anotherArr[10];

test1( anotherArr );
test2( anotherArr );
}


Share this post


Link to post
Share on other sites
Quote:
though I believe a references are already const
No. In fact, with a reference, it is now possible to modify the original object (as you have a reference, not a value copy). Return by reference avoids copying semantics. It doesn't provide constancy.

Share this post


Link to post
Share on other sites
Quote:
Original post by oler1s
Quote:
though I believe a references are already const
No. In fact, with a reference, it is now possible to modify the original object (as you have a reference, not a value copy). Return by reference avoids copying semantics. It doesn't provide constancy.


I meant that the reference itself is a reference to the fred object it is initialized to, and cannot be used to refer to another fred object. I suppose this means instead that the reference returned cannot be used to call non const member functions?

Share this post


Link to post
Share on other sites
Quote:
Original post by Burnt_Fyr
The first is the same as saying const Fred&, AFAIK, which specifies that the reference to fred is immutable, though I believe a references are already const, someone more knowledgeable will correct me if I'm wrong.


You can't change which variable/object is being referenced once its been initialized, but the variable or object you are referencing can be const or non-const.

Share this post


Link to post
Share on other sites
Great replies chaps thanks! Just need to print them off and then study through them. I intend to be back on later to ask more questions thanks ;o)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!