overloading ()

L

luca regini

I have this code

class M
{
.....

T operator()( size_t x, size_t y ) const
{ ... Operator overloading A ....}

T& operator()( size_t x, size_t y )
{ ... Operator overloading B .... }

}

Then i use the following code

M m;

int i=m(0,0); // Call A
m(0,0)=5; //Call B

I expect "Call A" to cause "Operator overloading A" to be called and
"Call B" to cause "Operator Overloading B" to be called.
What happens under visual studio 7.0 is that Operator overloading B is
always called.
Is this correct?

Thanks in advance,
Luca Regini
 
V

Victor Bazarov

luca said:
I have this code

class M
{
....

T operator()( size_t x, size_t y ) const
{ ... Operator overloading A ....}

T& operator()( size_t x, size_t y )
{ ... Operator overloading B .... }

}

Then i use the following code

M m;

int i=m(0,0); // Call A
m(0,0)=5; //Call B

I expect "Call A" to cause "Operator overloading A" to be called and
"Call B" to cause "Operator Overloading B" to be called.
What happens under visual studio 7.0 is that Operator overloading B is
always called.
Is this correct?

It will happen everywhere, not only in VS 7. Since your 'm' is not
constant, the non-const variation of operator() will be called.

V
 
A

Alf P. Steinbach

* luca regini:
I have this code

class M
{
....

T operator()( size_t x, size_t y ) const
{ ... Operator overloading A ....}

T& operator()( size_t x, size_t y )
{ ... Operator overloading B .... }

}

Then i use the following code

M m;

int i=m(0,0); // Call A
m(0,0)=5; //Call B

I expect "Call A" to cause "Operator overloading A" to be called and
"Call B" to cause "Operator Overloading B" to be called.
What happens under visual studio 7.0 is that Operator overloading B is
always called.
Is this correct?

The result type is not used to determine which function to call, only
the arguments. For member functions you can regard the 'this' pointer
as an argument in essentially the same way as others, and the difference
is that for function A you have an 'M const*' argument, whereas for B
you have an 'M*' argument. Call the function on a const object and
you'll invoke function A.
 
R

Ron Natalie

luca said:
I have this code

class M
{
....

T operator()( size_t x, size_t y ) const
{ ... Operator overloading A ....}

T& operator()( size_t x, size_t y )
{ ... Operator overloading B .... }

}

Then i use the following code

M m;

int i=m(0,0); // Call A
m(0,0)=5; //Call B

I expect "Call A" to cause "Operator overloading A" to be called and
"Call B" to cause "Operator Overloading B" to be called.
What happens under visual studio 7.0 is that Operator overloading B is
always called.
Is this correct?
The object m is NOT const, so the non-const overload is always
called. Whether you are using the return value (or what the
return value is) never plays a part in the selection of an function
overload.

The const operator would only be selected if m were const.
 
M

Marcus Kwok

luca regini said:
I have this code

class M
{
....

T operator()( size_t x, size_t y ) const
{ ... Operator overloading A ....}

T& operator()( size_t x, size_t y )
{ ... Operator overloading B .... }

}

Then i use the following code

M m;

int i=m(0,0); // Call A
m(0,0)=5; //Call B

I expect "Call A" to cause "Operator overloading A" to be called and
"Call B" to cause "Operator Overloading B" to be called.
What happens under visual studio 7.0 is that Operator overloading B is
always called.
Is this correct?

Others have explained why this is correct. If you wish to override
this, you could const_cast it:

int i = const_cast<const M&>(m)(0, 0); // Call A
 
L

luca regini

Thanks everyone for the nice explanation....
Then i have one more question: i would like the () operator to behave
differently if the object is used as a left or right value. Is there
any way to do this???
Greets,
Luca



Marcus Kwok ha scritto:
 

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
474,266
Messages
2,571,081
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top