473,486 Members | 2,407 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Comparing 2 references in C++

Hi,

I have a function which takes 2 references of A:

void f1 (const A& a1, const A& a2) {
if (a1 == a2)
return;
// do something else
}

My question is does this line 'a1 == a2' does an address check? or it
will calls the overloaded == operator of A?

For example, if I do this:
A* anA = new A();

f1 (*anA, *anA);

// will the overloaded == operator of A gets called? or it will not
get call since *anA points to the same address, "a1 == a2" is true.

Thank you.

Feb 23 '07 #1
3 15460
* si***************@gmail.com:
>
I have a function which takes 2 references of A:

void f1 (const A& a1, const A& a2) {
if (a1 == a2)
return;
// do something else
}

My question is does this line 'a1 == a2' does an address check? or it
will calls the overloaded == operator of A?
The latter.

For example, if I do this:
A* anA = new A();

f1 (*anA, *anA);

// will the overloaded == operator of A gets called? or it will not
get call since *anA points to the same address, "a1 == a2" is true.
How the function is called doesn't matter.

To do an address check do &a1 == &a2, assuming operator& isn't overloaded.

--
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?
Feb 23 '07 #2
si***************@gmail.com wrote:
Hi,

I have a function which takes 2 references of A:

void f1 (const A& a1, const A& a2) {
if (a1 == a2)
return;
// do something else
}

My question is does this line 'a1 == a2' does an address check? or it
will calls the overloaded == operator of A?
It uses operator==. If you want an address check you can compare &a1 ==
&a2.
>
For example, if I do this:
A* anA = new A();

f1 (*anA, *anA);

// will the overloaded == operator of A gets called? or it will not
get call since *anA points to the same address, "a1 == a2" is true.

Thank you.
Feb 23 '07 #3
si***************@gmail.com wrote:
Hi,

I have a function which takes 2 references of A:

void f1 (const A& a1, const A& a2) {
if (a1 == a2)
return;
// do something else
}

My question is does this line 'a1 == a2' does an address check? or it
will calls the overloaded == operator of A?
It will call the overloaded == operator of A (if there is one)
If you want to compare addresses of the references, you can just
take its address.

if( &a1 == &a2 ) // this will compare the addresses
>
For example, if I do this:
A* anA = new A();

f1 (*anA, *anA);

// will the overloaded == operator of A gets called? or it will not
get call since *anA points to the same address, "a1 == a2" is true.

Thank you.
Feb 23 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
495
by: Iain | last post by:
Hi Hopefully I am missing something really simple with this question, but here goes. I have two Bitarrays that I would like to compare. At the moment, I am XORing one with the other and...
1
1674
by: nydiroth | last post by:
I am trying to use the following code to reset a custom counter: (I forgot where I got the code but it's not my own and I did not remove and credits, this is how it came) Function...
2
2025
by: Raphael Iloh | last post by:
Hi all, I'm having problems comparing array objects. Take a look at this: int array1 = new int{1}; int array2 = new int{1}; Console.Writeln(array1.Equals(array2)); One would expect the above...
1
2466
by: - Steve - | last post by:
I've assigned three pictures as resources. So I can access them via Resources.Image1, Resources.Image2, etc On the buttons I try to do the following if(Button1.Image == Resources.Image1) ...
4
1325
by: - Steve - | last post by:
The following line of vb.net code works fine: if(strAnswer.toUpper() = strUserAnswer.toUpper()) However in an ASP.net page I'm told Object reference not set to an instance of an object ...
19
2616
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
7
5164
by: andreas | last post by:
Hello, I have a problem with iterators in a fairly complex polygonal mesh data structure which is implemented using lists of geometric entities. However, the problem in itself is fairly simple:...
11
1779
by: Xiaoshen Li | last post by:
Dear Sir, I am a little puzzled about a function returning a class object, for example, suppose I hava a class Money and a method: Money lastYear(Money aMoney) { Money tempMoney; ......
20
2252
by: Aaron Gray | last post by:
There does not seem too be anyway to test if two jQuery references are the same element. Given :- ... <div id="1"></div .... Then :- alert( $("#1") == $("#1"))
0
7100
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
6964
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
7126
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,...
1
6842
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...
0
5434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4865
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.