• Popular Now

• 10
• 9
• 12
• 14
• 15

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

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 on other sites
You still have more reading to do.

Share on other sites
Thanks for the quick reply. That's actually the link I'm reading through and I'm referring to what's going on at part 18[12].

Share on other sites
Quote:
 Original post by adder_noirI 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 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 on other sites
Quote:
 Original post by adder_noirI 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 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 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 on other sites
Quote:
 Original post by Burnt_FyrThe 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.