0

Possible Duplicate:
How to pass objects to functions in C++?
Operator & and * at function prototype in class

#include <iostream>
using namespace std;

class C {
  public:
    int isSelf (C& param);
};

bool C::isSelf (C& param)
{
  if (&param == this) return true;
  else return false;
}

int main () {
  C a;
  C* b = &a;
  cout << boolalpha << b->isSelf(a) << endl;
  return 0;
}

This code works. But it seems to me that b->isSelf(a) should really be b -> isSelf(&a) because isSelf expects an address of type C?!

[EDIT] Additional questions:

1) Is there a way to implement this isSelf function using pass by value? 2) Are the implementation using pass by reference and pass by pointer correct?

bool C::isSelf1(const C &c)
{
    if (&c == this) {
        return true;
    } else {
        return false;
    }
}

bool C::isSelf2(C c)
{
    if (&c == this) {
        return true;
    } else {
        return false;
    }
}

bool C::isSelf3(C * c)
{
    if (c == this) {
        return true;
    } else {
        return false;
    }
}

int main ()
{
    C c1 (2);
    C * c2 = &c1;
    cout << boolalpha;
    cout << c2 -> isSelf1(c1) << endl; // pass by reference
    cout << c2 -> isSelf2(c1) << endl; // pass by value
    cout << c2 -> isSelf3(&c1) << endl;// pass by pointer
    return 0;
}
Community
  • 1
  • 1
qazwsx
  • 25,536
  • 30
  • 72
  • 106

5 Answers5

6

There is a difference between:

bool C::isSelf (C& param)  // pass by 'reference'
               ^^^

and

bool C::isSelf (C* param)  // pass by 'address'
               ^^^

So in 2nd version address of C object (i.e. C*) is expected and not in 1st version.

Also note that, internally 1st and 2nd versions might be implemented similarly; but there is a syntax difference. There is a 3rd version:

bool C::isSelf (C param)  // pass by value
               ^^^

For edited question:

1) Is there a way to implement this isSelf() function using pass by value?

Not possible for your given requirement. Because it creates a new value and that would never match. Remove pass by value version from your code.

Apart from that, in general you should choose either pass by value or pass by reference, for any function. You can't have both, because function call syntax for both of them would be same, which will result in ambiguity.

2) Are the implementation using pass by reference and pass by pointer correct?

They are correct, but you can simplify them as:

bool C::isSelf(const C &c) const // don't give postfix '1', simply overload
{                          ^^^^^
  return (&c == this);
}

bool C::isSelf(C* const c) const // don't give postfix '3', simply overload
{                          ^^^^^
  return (c == this);
}

Also, see the const correct-ness syntax of doing such things.

iammilind
  • 68,093
  • 33
  • 169
  • 336
3

Q: But it seems to me that b->isSelf(a) should really be b -> isSelf(&a)

A: Yes, that's how the "addressof" operator (&) works. But this is a reference parameter.

Check out this example in this link:

http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

void foo(int &y) // y is now a reference
{
    using namespace std;
    cout << "y = " << y << endl;
    y = 6;
    cout << "y = " << y << endl;
} // y is destroyed here

int main()
{
    int x = 5;
    cout << "x = " << x << endl;
    foo(x);
    cout << "x = " << x << endl;
    return 0;
}

PS:

Here's the sample output from above:

x = 5
y = 5
y = 6
x = 6
paulsm4
  • 114,292
  • 17
  • 138
  • 190
1

In a function declaration, an ampersand before a parameter name indicates passing by reference. In C, this can only be accomplished through pointers. With passing by reference, though, the caller need not know the argument is passed by reference and simply uses the object as normal.

It is just an unfortunate coincidence that the & character is used for both address-of and reference parameter declaration. Remember that address passing is declared like this: int isSelf(C *other);

Platinum Azure
  • 45,269
  • 12
  • 110
  • 134
0

The & in function signature implies that the value will be passed as an Alias, that is, it will be the exact memory location of the original variable.

This is little bit different from passing address, where the parameter type itself is a Pointer to your type, as opposed to an exact type.

Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176
0

In C++, a function that has an argument like C& param passes it by reference instead of value. This is similar to how one might usually pass in a pointer to a function, but it helps prevent some of the common mistakes in pointer operations.

You might want to take a look at the beginning of this link: http://www.cplusplus.com/doc/tutorial/functions2/

When a variable is passed by reference we are not passing a copy of its value, but we are somehow passing the variable itself to the function and any modification that we do to the local variables will have an effect in their counterpart variables passed as arguments in the call to the function.

Roy
  • 3,574
  • 2
  • 29
  • 39