subscript overloading

J

josh

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::eek:perator[]( int subscript )
{
// check for subscript out of range error
assert( 0 <= subscript && subscript < size );

return ptr[ subscript ]; // reference return
}

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

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

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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::eek:perator[]( int subscript )
{
// check for subscript out of range error
assert( 0 <= subscript && subscript < size );

return ptr[ subscript ]; // reference return

}

const int &Array::eek:perator[]( 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
}
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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::eek:perator[]( int subscript )
{
// check for subscript out of range error
assert( 0 <= subscript && subscript < size );
return ptr[ subscript ]; // reference return

const int &Array::eek:perator[]( 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."
 
J

josh

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::eek:perator[]( int subscript )
{
// check for subscript out of range error
assert( 0 <= subscript && subscript < size );
return ptr[ subscript ]; // reference return
}
const int &Array::eek:perator[]( 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."

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?
 
F

Fei Liu

josh said:
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::eek:perator[]( int subscript )
{
// check for subscript out of range error
assert( 0 <= subscript && subscript < size );
return ptr[ subscript ]; // reference return
}
const int &Array::eek:perator[]( 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."

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::eek:stream & operator << (std::eek:stream & os, const T & t);

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

Fei
 
F

Fei Liu

Fei said:
josh said:
On Feb 16, 11:34 am, "Erik Wikström" <[email protected]>
wrote:



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::eek:perator[]( int subscript )
{
// check for subscript out of range error
assert( 0 <= subscript && subscript < size );
return ptr[ subscript ]; // reference return
}
const int &Array::eek:perator[]( 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."

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::eek:stream & operator << (std::eek:stream & os, const T & t);

std::eek:stream & operator<< <int>(std::eek:stream &, 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::eek:stream & operator << (std::eek:stream & os, const T & x){
return os << x;
}
}
namespace composite{
template <typename T>
std::eek:stream & operator << (std::eek:stream & os, const T & x){
return std::eek:perator << (os, x);
}
}

int main(){

int x = 3;
intrinsic::eek:perator << (std::cout, x);
intrinsic::eek:perator << <int>(std::cout, x);
composite::eek:perator << <std::string>(std::cout, "hello, world");
std::string y = "Hello, world";
composite::eek:perator << (std::cout, y);
}
 
M

Marcus Kwok

josh said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top