By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,534 Members | 1,811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,534 IT Pros & Developers. It's quick & easy.

subscript overloading

P: n/a
Hi I've a dubt!

when we have overloaded functions the compiler chooses the right
being based on the argument lists...but when we have two subscript
overloaded functions it resolves them being based on the const type.
Infact if I use the Array on the left side i.e like a1[2] = 111
then it uses the first while if I use cout << a1[2] it uses the
second...
why????

int &Array::operator[]( int subscript )
{
// check for subscript out of range error
assert( 0 <= subscript && subscript < size );

return ptr[ subscript ]; // reference return
}

const int &Array::operator[]( int subscript ) const
{
// check for subscript out of range error
assert( 0 <= subscript && subscript < size );

return ptr[ subscript ]; // const reference return
}

Feb 16 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Feb 16, 10:35 am, "josh" <xdevel2...@yahoo.comwrote:
Hi I've a dubt!

when we have overloaded functions the compiler chooses the right
being based on the argument lists...but when we have two subscript
overloaded functions it resolves them being based on the const type.
Infact if I use the Array on the left side i.e like a1[2] = 111
then it uses the first while if I use cout << a1[2] it uses the
second...
why????

int &Array::operator[]( int subscript )
{
// check for subscript out of range error
assert( 0 <= subscript && subscript < size );

return ptr[ subscript ]; // reference return

}

const int &Array::operator[]( int subscript ) const
{
// check for subscript out of range error
assert( 0 <= subscript && subscript < size );

return ptr[ subscript ]; // const reference return

}
The const version is used when the object who's function/operator is
called. Consider the following two functions:

void foo(Array& a) {
int i = a[0]; // Non-const used
}

void bar(const Array& a)
int i = a[0]; // Const used
}

--
Erik Wikström

Feb 16 '07 #2

P: n/a
On Feb 16, 11:34 am, "Erik Wikström" <eri...@student.chalmers.se>
wrote:
On Feb 16, 10:35 am, "josh" <xdevel2...@yahoo.comwrote:
Hi I've a dubt!
when we have overloaded functions the compiler chooses the right
being based on the argument lists...but when we have two subscript
overloaded functions it resolves them being based on the const type.
Infact if I use the Array on the left side i.e like a1[2] = 111
then it uses the first while if I use cout << a1[2] it uses the
second...
why????
int &Array::operator[]( int subscript )
{
// check for subscript out of range error
assert( 0 <= subscript && subscript < size );
return ptr[ subscript ]; // reference return
}
const int &Array::operator[]( int subscript ) const
{
// check for subscript out of range error
assert( 0 <= subscript && subscript < size );
return ptr[ subscript ]; // const reference return
}

The const version is used when the object who's function/operator is
called. Consider the following two functions:
What I meant was that "The const version is used when the object who's
function/operator is called is const."

--
Erik Wikström
Feb 16 '07 #3

P: n/a
On 16 Feb, 11:41, "Erik Wikström" <eri...@student.chalmers.sewrote:
On Feb 16, 11:34 am, "Erik Wikström" <eri...@student.chalmers.se>
wrote:
On Feb 16, 10:35 am, "josh" <xdevel2...@yahoo.comwrote:
Hi I've a dubt!
when we have overloaded functions the compiler chooses the right
being based on the argument lists...but when we have two subscript
overloaded functions it resolves them being based on the const type.
Infact if I use the Array on the left side i.e like a1[2] = 111
then it uses the first while if I use cout << a1[2] it uses the
second...
why????
int &Array::operator[]( int subscript )
{
// check for subscript out of range error
assert( 0 <= subscript && subscript < size );
return ptr[ subscript ]; // reference return
}
const int &Array::operator[]( int subscript ) const
{
// check for subscript out of range error
assert( 0 <= subscript && subscript < size );
return ptr[ subscript ]; // const reference return
}
The const version is used when the object who's function/operator is
called. Consider the following two functions:

What I meant was that "The const version is used when the object who's
function/operator is called is const."

--
Erik Wikström
Ok I understand that but in my code I have:

Array integers1( 7 ); // non const

// use overloaded subscript operator to create rvalue
cout << "integers1[5] is " << integers1[ 5 ] << '\n';

// use overloaded subscript operator to create lvalue
cout << "Assigning 1000 to integers1[5]\n";
integers1[ 5 ] = 1000;

so integers1 is not declared as const and then how the compiler can
know
what must be used?

Feb 16 '07 #4

P: n/a
josh wrote:
On 16 Feb, 11:41, "Erik Wikström" <eri...@student.chalmers.sewrote:
>On Feb 16, 11:34 am, "Erik Wikström" <eri...@student.chalmers.se>
wrote:
>>On Feb 16, 10:35 am, "josh" <xdevel2...@yahoo.comwrote:
Hi I've a dubt!
when we have overloaded functions the compiler chooses the right
being based on the argument lists...but when we have two subscript
overloaded functions it resolves them being based on the const type.
Infact if I use the Array on the left side i.e like a1[2] = 111
then it uses the first while if I use cout << a1[2] it uses the
second...
why????
int &Array::operator[]( int subscript )
{
// check for subscript out of range error
assert( 0 <= subscript && subscript < size );
return ptr[ subscript ]; // reference return
}
const int &Array::operator[]( int subscript ) const
{
// check for subscript out of range error
assert( 0 <= subscript && subscript < size );
return ptr[ subscript ]; // const reference return
}
The const version is used when the object who's function/operator is
called. Consider the following two functions:
What I meant was that "The const version is used when the object who's
function/operator is called is const."

--
Erik Wikström

Ok I understand that but in my code I have:

Array integers1( 7 ); // non const

// use overloaded subscript operator to create rvalue
cout << "integers1[5] is " << integers1[ 5 ] << '\n';

// use overloaded subscript operator to create lvalue
cout << "Assigning 1000 to integers1[5]\n";
integers1[ 5 ] = 1000;

so integers1 is not declared as const and then how the compiler can
know
what must be used?
cout << integers1[5] is translated to:
function prototype: template <typename T>
std::ostream & operator << (std::ostream & os, const T & t);

std::ostream & operator<< <int>(std::ostream &, const int & integers1[5]);

Fei

Feb 16 '07 #5

P: n/a
Fei Liu wrote:
josh wrote:
>On 16 Feb, 11:41, "Erik Wikström" <eri...@student.chalmers.sewrote:
>>On Feb 16, 11:34 am, "Erik Wikström" <eri...@student.chalmers.se>
wrote:

On Feb 16, 10:35 am, "josh" <xdevel2...@yahoo.comwrote:
Hi I've a dubt!
when we have overloaded functions the compiler chooses the right
being based on the argument lists...but when we have two subscript
overloaded functions it resolves them being based on the const type.
Infact if I use the Array on the left side i.e like a1[2] = 111
then it uses the first while if I use cout << a1[2] it uses the
second...
why????
int &Array::operator[]( int subscript )
{
// check for subscript out of range error
assert( 0 <= subscript && subscript < size );
return ptr[ subscript ]; // reference return
}
const int &Array::operator[]( int subscript ) const
{
// check for subscript out of range error
assert( 0 <= subscript && subscript < size );
return ptr[ subscript ]; // const reference return
}
The const version is used when the object who's function/operator is
called. Consider the following two functions:
What I meant was that "The const version is used when the object who's
function/operator is called is const."

--
Erik Wikström

Ok I understand that but in my code I have:

Array integers1( 7 ); // non const

// use overloaded subscript operator to create rvalue
cout << "integers1[5] is " << integers1[ 5 ] << '\n';

// use overloaded subscript operator to create lvalue
cout << "Assigning 1000 to integers1[5]\n";
integers1[ 5 ] = 1000;

so integers1 is not declared as const and then how the compiler can
know
what must be used?

cout << integers1[5] is translated to:
function prototype: template <typename T>
std::ostream & operator << (std::ostream & os, const T & t);

std::ostream & operator<< <int>(std::ostream &, const int & integers1[5]);

Fei
Check attached example to understand what's going on beneath the hood:

#include <iostream>
#include <string>
namespace intrinsic{
template <typename T>
std::ostream & operator << (std::ostream & os, const T & x){
return os << x;
}
}
namespace composite{
template <typename T>
std::ostream & operator << (std::ostream & os, const T & x){
return std::operator << (os, x);
}
}

int main(){

int x = 3;
intrinsic::operator << (std::cout, x);
intrinsic::operator << <int>(std::cout, x);
composite::operator << <std::string>(std::cout, "hello, world");
std::string y = "Hello, world";
composite::operator << (std::cout, y);
}
Feb 16 '07 #6

P: n/a
josh <xd********@yahoo.comwrote:
Ok I understand that but in my code I have:

Array integers1( 7 ); // non const

// use overloaded subscript operator to create rvalue
cout << "integers1[5] is " << integers1[ 5 ] << '\n';

// use overloaded subscript operator to create lvalue
cout << "Assigning 1000 to integers1[5]\n";
integers1[ 5 ] = 1000;

so integers1 is not declared as const and then how the compiler can
know
what must be used?
In both cases, the non-const version will be called, since the original
integers1 object is non-const.

To check, you could add an output statement (e.g., cout << "non-const";
or cout << "const";) to both of the subscript operators to verify which
one is called.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Feb 16 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.