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

Comparing 2 references in C++

P: n/a
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
Share this Question
Share on Google+
3 Replies


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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.