473,395 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

overloading ()

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
5 2461
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
* 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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Terje Slettebų | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
39
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any...
45
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
31
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
15
by: lordkain | last post by:
is it possible to do some kind of function overloading in c? and that the return type is different
11
by: placid | last post by:
Hi all, Is it possible to be able to do the following in Python? class Test: def __init__(self): pass def puts(self, str): print str
10
by: Matthew | last post by:
Am I correct in thinking there is no method/function overloading of any kind in any version of PHP? Thanks, Matthew
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.