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

overloading ()

P: n/a
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

Aug 31 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
luca regini wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 31 '06 #2

P: n/a
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 31 '06 #3

P: n/a
luca regini wrote:
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.
Aug 31 '06 #4

P: n/a
luca regini <lu*********@gmail.comwrote:
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

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Aug 31 '06 #5

P: n/a
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:
luca regini <lu*********@gmail.comwrote:
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

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Sep 3 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.